Discord Bot Can't Get Channel by Name

前端 未结 4 599
盖世英雄少女心
盖世英雄少女心 2020-12-03 21:52

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.

         


        
相关标签:
4条回答
  • 2020-12-03 22:01

    You can also use

    bot.channels.find("name","welcome").send("Welcome!")
    
    0 讨论(0)
  • 2020-12-03 22:05

    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 Appearance

    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.

    0 讨论(0)
  • 2020-12-03 22:10

    Your error could come from the fact that you're using bot.channels.get(), which isn't the best idea, because discord.js isn't very friendly when it comes to using .send() on multiple items.

    Instead, try to use member.guild.channels.find("name", "channel").send();, if possible. If this is in the client.on("message"), then simply use message.member.channels.find("name", "channel").send();

    Sidenote: My memory is mixed up, so if that doesn't work, try .get() instead of find.

    0 讨论(0)
  • 2020-12-03 22:17

    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!");
    
    0 讨论(0)
提交回复
热议问题