如何解决如何检查消息是否包含频道提及Discord.py
这是我的代码,我想通过discord.py创建一条命令,该命令以“ say [message] ”写一条消息,并以“ say [频道] [消息] ”。在大多数情况下,我都知道了。我的问题是我想检查命令“ 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和其他一些方法,但是我无法弄清楚。
解决方法
我们可以使用一些精美的converter 功能来使命令解析机制为我们完成此操作:
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()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。