将频道提及转换为不和谐的文字频道对象

时间:2020-10-16 03:22:57

标签: python discord.py-rewrite

问题的措词可能很奇怪,但是我试图让一个函数将频道提及和消息(字符串)作为参数。然后将消息发送到提到的频道。此函数在类(cog)中,因此这就是self的原因:

# Discord Imports
import discord
from discord.ext import commands
from discord.utils import get

class Author_Only(commands.Cog):
  def __init__(self,bot):
    self.bot=bot

  @commands.command(
  brief='Sends a message using Doge Bot')
  async def announce(self,ctx,channel,*,message:str): # I can't seem to find the proper conversion for a text channel, 
    await ctx.message.delete()                        # since you can do a similar thing with mentioning someone
    await channel.send(message)

1 个答案:

答案 0 :(得分:1)

您显然已经知道如何确保消息是字符串,而对于频道而言,恰恰是

str

discord.TextChannel

以下示例

#Discord Imports
import discord
from discord.ext import commands
from discord.utils import get

class Author_Only(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
    
    @commands.command(brief="Sends a message using doge bot")
    async def announce(self, ctx, channel: discord.Channel, *, message: str):
        await ctx.message.delete()
        await channel.send(message)