繁体   English   中英

discord.py:如何获取提到的频道的频道 ID?

[英]discord.py: How to get channel id of a mentioned channel?

我现在正在编写一个 discord 机器人。 但我有一个问题,我不知道如何获取提到的频道的频道 ID。

我怎样才能得到身份证?

例子:

def check(messagehchannelid):
     return messagehchannelid.channel.id == ctx.message.channel.id and messagehchannelid.author == ctx.message.author and messagehchannelid.content == (the channel id of the mentioned channel in the message)

messagechannelidcheck = await client.wait_for('message', check=check, timeout=None)

使用命令装饰器的示例:

@client.command()
async def cmd(ctx, channel: discord.TextChannel):
    await ctx.send(f"Here's your mentioned channel ID: {channel.id}")

后期编辑:
您可以使用消息的channel_mentions属性来查看提到了哪些频道。 如果你只期待一个,你可以这样做:

# making sure they've mentioned a channel, and replying in the same channel
# the command was executed in, and by the same author
def check(msg):
    return len(msg.channel_mentions) != 0 and msg.channel == ctx.channel and
    ctx.author == msg.author

msg = await client.wait_for("message", check=check) # timeout is None by default
channel_id = msg.channel_mentions[0].id

参考:

暂无
暂无

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

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