Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set channel object using the channel id in discord.py?

Hello guys I'm trying to make a function that checks if few channels in the my discord server are full but I didn't know how to specify them. The best idea I had was using the idea but couldn't set it to a channel object so how can I do that or is there any other good ideas?

def full(channel):
   while True:
       if len(channel.voice_members) == channel.user_limit:
           print("Wooo There is a full channel.")
       asyncio.sleep(10)

But I didn't know what to set the channel parameter. Thanks in advance for your help.

like image 984
ADHAM Al-Moqdad Avatar asked Sep 03 '25 09:09

ADHAM Al-Moqdad


2 Answers

You can get the channel object either by ID or by name.

Use discord.utils.get() to return a channel by its name (example for voice channels):

channel = discord.utils.get(server.channels, name="Channel_name_here", type="ChannelType.voice") 

Or you can use discord.get_channel(id) if you have the ID of the channel.

So for example if you have a list of channel names you want to check:

channel_names = ['channel1', 'channel2', 'channel3']
for ch in channel_names:
    channel = discord.utils.get(server.channels, name=ch, type="ChannelType.voice")
    full(channel)

See more in the documentation:

discord.get_channel()

discord.utils.get()

like image 178
Fanatique Avatar answered Sep 04 '25 21:09

Fanatique


If you want this to be a command, you can also take advantage of converters:

@bot.command(pass_context=True)
async def full(ctx, channel: discord.Channel):
    if len(channel.voice_members) == channel.user_limit:
        await bot.say("{} is full".format(channel.name))
    else:
        await bot.say("{} is not full".format(channel.name))
like image 31
Patrick Haugh Avatar answered Sep 04 '25 21:09

Patrick Haugh