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

时间:2020-06-05 14:35:47

标签: python discord.py

我正在编码一个不和谐的机器人。但是我有一个问题,我不知道如何获取所提及频道的频道ID。

如何获取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)

1 个答案:

答案 0 :(得分:1)

使用命令修饰符的示例:

@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

参考: