Reputation: 82
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.
Upvotes: 3
Views: 18392
Reputation: 748
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');
Upvotes: 1
Reputation: 1803
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.
Upvotes: 6
Reputation: 233
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")
})
Upvotes: 4
Reputation: 711
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):
welcome
or general
whichever you prefer the bot to send it to.guild.channels.find(`name`,`welcome`).send(`Thx for invite`);
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:
return -1
return -1
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)
Upvotes: 3