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

Question:

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)
Asked By: puncher

||

Answers:

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:

Answered By: Diggy.
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.