所以這是我的代碼,我想通過 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 和其他幾個,但我無法弄清楚。
1 回答

精慕HU
TA貢獻1845條經驗 獲得超8個贊
我們可以使用一些奇特的轉換器功能讓命令解析機制為我們做這件事:
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()
添加回答
舉報
0/150
提交
取消