简体   繁体   中英

Sending a message the first channel with discord.js

I've seen a lot of bots that have a greeting message when they join. Its usually sent to the first channel of the server.

For example:

bot joins the guild

Bot: Hey! Thanks for inviting me!

The code i have right now is really simple:

client.on("guildCreate", guild => {
    the code goes here
});

I dont know how to send a message to a random (first) channel though. If anyone could please tell me how the other bots do it I would be really greatful.

Edit: as of 9-27-2020, using Discord.js version 12.3.1, I only needed to add a reference to cache to get the correct channel and make sure the iteration was proceeding correctly.

I'm using Discord.js version 11.4.2, and I was able to use the following code to send a default message to the #general channel. Replace bot below with whatever your client 's name is.

const Discord = require('discord.js');
const bot = new Discord.Client();

bot.on("guildCreate", guild => {
    let channelID;
    let channels = guild.channels.cache;

    channelLoop:
    for (let key in channels) {
        let c = channels[key];
        if (c[1].type === "text") {
            channelID = c[0];
            break channelLoop;
        }
    }

    let channel = guild.channels.cache.get(guild.systemChannelID || channelID);
    channel.send(`Thanks for inviting me into this server!`);
});

Edit: I've found that the guild.systemChannelID can be null so I've updated the code to search for the first text channel and use that for the initial message.

I know is late but this is the way what I use and works for the 12.x version

client.on('guildCreate', guild => {
    const channel = guild.channels.cache.find(channel => channel.type === 'text' && channel.permissionsFor(guild.me).has('SEND_MESSAGES'))
    channel.send("Thanks for invite me")
})

To be quite frank there is no "good way" todo this... There used to be until discord removed it. (used to be: guild.defaultChannel )
Now days bot devs do some tests to find out where they send the message...

There are multiple ways todo find a good channel (here's a few):

A: Find a channel called welcome or general whichever you prefer the bot to send it to.

guild.channels.find(`name`,`welcome`).send(`Thx for invite`);

B: Send to the first channel the bot is allowed to send to. (this works well unless bot is given admin);

guild.channels.sort(function(chan1,chan2){
    if(chan1.type!==`text`) return 1;
    if(!chan1.permissionsFor(guild.me).has(`SEND_MESSAGES`)) return -1;
    return chan1.position < chan2.position ? -1 : 1;
}).first().send(`Thx! for invite`);

The above code does this:

  1. grab all the channels and lets sort them
  2. if it's not a text channel return -1
  3. if the bot is DIS-ALLOWED to send return -1
  4. then sort the channels based on their position

C: Try other ways:

Find a channel most members in the server can send to
Find a channel the role @everyone can send to guild.roles.first()
Find a channel that gets the most activity (check for channel with most messages sent in the past 5-10 minutes)

What i was struggling with was to find get the guild. And also other codes weren't working for me.

So you need to find guild like this

const guild = client.guilds.cache.first();

Then you want to get the first channel like this (probably only 'text channel', if not just remove the filter() )

const channel = guild.channels.cache.filter(c => c.type === 'text').find(x => x.position === 0);

Then its all up to you what you want to do... I wanted to send a message to the channel for example, so i just did this

channel.send('Hello world');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM