繁体   English   中英

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

[英]How to check if a message contains a channel mention (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 和其他一些,但我无法弄清楚。

我们可以使用一些花哨的转换器功能让命令解析机器为我们执行此操作:

from typing import Optional
from discord import TextChannel

@client.command(aliases=['echo', 'print'], description="say <message>")
async def say(ctx, channel: Optional[TextChannel], *, message=""):
    channel = channel or ctx  # default to ctx if we couldn't detect a channel
    await channel.send(message)
    await ctx.message.delete()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM