Is it possible to get channel ID by name in discord.py

Viewed 43276

The title says it all... I want to get the channel id given a specific name in a guild, but I could not find anything in the documentation that lets me do that. Looking at more developed bots like Mee6, they have the option to connect to your server and set a welcome page for any of your text channels. I've tried something like:

channels = discord.utils.get(client.get_all_channels(), guild__name='Test Server'

But this only returns 'Text Channels' and nothing else.

5 Answers

What Abdulaziz said is correct but is less efficient and lengthier, you can use utils.get

channel = discord.utils.get(ctx.guild.channels, name=given_name)
channel_id = channel.id

If you don't specifically know what server you are looking through to get the channel, you could use this:

channel = discord.utils.get(self.client.get_all_channels(), id=channel_id)
channel = discord.utils.get(client.get_all_channels(), name=name_channel)

If you are using discord.ext.commands you can do a loop like this.

@bot.command()
async def get_channel(ctx, *, given_name=None):
    for channel in ctx.guild.channels:
        if channel.name == given_name:
            wanted_channel_id = channel.id

    await ctx.send(wanted_channel_id) # this is just to check 
Related