如何检查消息是否包含频道提及 (Discord.py)

所以这是我的代码,我想通过 discord.py 创建一个命令,用“say [message] ”写一条消息,并用“say [channel] [message] ”在频道中写一条消息。在大多数情况下,我把它弄出来了。我遇到的问题是我想检查命令“say”之后的第一个参数是否是频道提及。


client = commands.Bot(command_prefix="_")  


@client.command(aliases=['echo', 'print'], description="say <message>")

    async def say(ctx, channel, *, message=""): 

        await ctx.message.delete()

    

        if not channel:

            await ctx.send("What do you want me to say?")

        else:

            if channel == discord.TextChannel.mention:

                await ctx.send("test")

            else:

                await ctx.send(str(channel) + " " + message) 

我已经尝试过使用 discord.textchannel、discord.message.channel_mentions 和其他几个,但我无法弄清楚。


DIEA
浏览 97回答 1
1回答

精慕HU

我们可以使用一些奇特的转换器功能让命令解析机制为我们做这件事:from typing import Optionalfrom discord import TextChannel@client.command(aliases=['echo', 'print'], description="say <message>")async def say(ctx, channel: Optional[TextChannel], *, message=""):&nbsp; &nbsp; channel = channel or ctx&nbsp; # default to ctx if we couldn't detect a channel&nbsp; &nbsp; await channel.send(message)&nbsp; &nbsp; await ctx.message.delete()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python