简体   繁体   English

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

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

I'm coding a discord bot right now.我现在正在编写一个 discord 机器人。 But I have the problem that I don't know how to get a channel id of a mentioned channel.但我有一个问题,我不知道如何获取提到的频道的频道 ID。

How can I get the ID?我怎样才能得到身份证?

example:例子:

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)

An example using command decorators:使用命令装饰器的示例:

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

Post-edit:后期编辑:
You can use the channel_mentions attribute of a message to see what channels have been mentioned.您可以使用消息的channel_mentions属性来查看提到了哪些频道。 If you're only expecting one, you can do:如果你只期待一个,你可以这样做:

# 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

References:参考:

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

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