簡體   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