10

how do I get the name of a channel so that this bot will work on any server its put on with no changes to code necessary? ( in the code where I put "what do I put here" is where I want the name to be in a variable)Thanks

from discord.ext.commands import Bot
import time, asyncio

TOKEN = 'Its a secret'
BOT_PREFIX = ["!"]
client = Bot(command_prefix=BOT_PREFIX)




@client.event
async def on_message(message):
    if message.author == client.user:
        return




@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')
    await start()
    while True:
        currentTime = time.strftime("%M%S", time.gmtime(time.time()))
        if currentTime == "30:00":
            await start()
        await asyncio.sleep(1)


async def start():
    mainChannel = #What do i put here?
    print(mainChannel.name)
    await client.send_message(mainChannel, "Starting countdown", tts = True)



client.run(TOKEN)
2
  • 1
    Are you trying to get the Channel object when all you know is its name? How do you choose which server you want to message? DO you have the id of this channel? Commented Oct 21, 2018 at 14:37
  • 1
    I want to send it to the main channel of all the servers the bot is in, so I don't have any ids. ideally it would look for a channel with the name "general" or "main channel"
    – Paletech35
    Commented Oct 25, 2018 at 13:30

4 Answers 4

14

Getting channel from ID (Recommended)

First, get the ID of the channel (Right click the channel and select "Copy ID")

Second, put the ID in the following code:

client.get_channel("ID")

For example:

client.get_channel("182583972662")

Note: The channel ID is a string in discord.py async, and an integer in rewrite

(Thanks to Ari24 for pointing this out)

Getting channel from name (Not reccomended)

First, get the server using either:

server = client.get_server("ID")

OR

for server in client.servers:
    if server.name == "Server name":
        break

Second, get the channel:

for channel in server.channels:
    if channel.name == "Channel name":
        break

What not to do

Try to always use the ID for each server, as it is much faster and more efficient.

Try to avoid using discord.utils.get, such as:

discord.utils.get(guild.text_channels, name="Channel name")

Although it does work, it is bad practise as it has to iterate through the entire list of channels. This can be slow and take much more time than using the ID.

From the discord API docs:

discord.utils.get is a helper that returns the first element in the iterable that meets all the traits passed in attrs

2
  • 3
    You should also include, that only in discord.py async the channel ID is a string and in rewrite it's an integer
    – Ari24
    Commented Oct 22, 2020 at 7:39
  • @Ari24 Thanks for pointing that out, I've added it to the answer
    – Rlz
    Commented Oct 30, 2020 at 16:58
7

Now in rewrite there's a method called discord.utils.get where you can actually getting discord objects with specific parameters

In your case with a channel name:

import discord
channel = discord.utils.get(guild.text_channels, name="Name of channel")

Should be None if discord couldn't find a textchannel with that name

2
  • what is guild assigned to
    – alper
    Commented Sep 28, 2021 at 18:55
  • @alper this depends on where you use it
    – Ari24
    Commented Oct 1, 2021 at 19:57
1

It is actually really easy: You can simply do message.channel.name

Example:

print(message.channel.name)
0

Well, it's simple to do. However your code can be improved by several way. It will be easier to read your code.

to get the channel and send a message to it, use this

ch = client.get_channel(channel id)
await ch.send("message")

few optimizations to your code

from discord.ext import commands
import discord
import time

TOKEN = "token"
client = commands.Client(command_prefix="!")

@client.event
async def on_ready():
    print(f"Logged in as {client.user}")
    print(f"ID: {client.id}")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.