Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discord Bot Can't Get Channel by Name

I have been making a discord bot and wanted to make it send a message to a specific "Welcome" channel. Unfortunately, I have been unable to do so. I tried this.

const welcomeChannel = bot.channels.get("name", "welcome")
welcomeChannel.sendMessage("Welcome\n"+member.user.username);

However in this "welcomeChannel is undefined".

Edit:

I tried using

const welcomeChannel = bot.channels.get("id", "18NUMBERIDHERE")
welcomeChannel.sendMessage("Welcome\n"+member.user.username);

but this is still undefined, strangely

like image 741
Leo Avatar asked Jan 06 '17 22:01

Leo


People also ask

How do I find my Discord channel by name?

Tl; dr: How to find Discord IDs To find a Server, Channel, or Message ID, right-click on the server/ channel name, or message, and select 'Copy ID'.

How do I set Discord bot to a specific channel?

Go to the Channel Permissions (right click->Edit Channel->Permissions) and add the bot user to the Roles/Members Permission list, then you can disable Read permissions for that specific user (or bot) so they are unable to see or interact with the channel.

How do I get text channel ID in Discord?

On Android press and hold the Server name above the channel list. You should see the last item on the drop-down menu: 'Copy ID'. Click Copy ID to get the ID.


3 Answers

You should use the channnel id instead of it's name.

How to get the channel id of a channel:

  1. Open up your Discord Settings

  2. Go to Advanced

  3. Tick Developer Mode (And close the Discord settings)

  4. Right click on your desired channel

  5. Now there's an option Copy ID to copy the channel id

Also checkout the discord.js documentation for (channel) collections


Furthermore your approach won't work because .get wants a channel id (see the linked documentation above). In case you REALLY want to get an channel by its name, use .find instead for that.
This is however a really bad idea in case your bot runs on more than one server since channel names can now occur multiple times.

like image 58
Der-Eddy Avatar answered Sep 28 '22 09:09

Der-Eddy


You can also use

bot.channels.find("name","welcome").send("Welcome!")
like image 37
Razboy20 Avatar answered Sep 28 '22 10:09

Razboy20


I tried a lot with the same error, and that's how I fixed it. I used client as my Client().

client.channels.cache.get("18NUMBERIDHERE").send("Welcome!");
like image 43
Bruno Bordón Avatar answered Sep 28 '22 09:09

Bruno Bordón