简体   繁体   中英

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

I'm coding a discord bot right now. But I have the problem that I don't know how to get a channel id of a mentioned channel.

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. 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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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