Reputation:
I'm trying to send a message to the annoucements channel when members join. This works if I don't use welcomeChannel
as a const and define it inside the function, but I don't want to do that.
const Discord = require('discord.js');
const bot = new Discord.Client();
bot.login('*****************************************');
const welcomeChannel = bot.channels.get("name", "annoucements") // also tried .find here
bot.on('guildMemberAdd', member => {
if (welcomeChannel) {
welcomeChannel.send(member + ', welcome to the server.');
}
});
Upvotes: 1
Views: 772
Reputation: 6816
I don't think there's a way to do that: you can't await client.login()
since it would be top-level, and you can't redefine a constant.
The only way you can keep using that value as a constant is by putting all the rest of your code inside the ready
event so that you don't have to change its value.
It would be way easier if you didn't use constants.
Also, you have to use guild.channels.find('name', 'channel_name')
, since .get()
only works with IDs
Upvotes: 0
Reputation: 696
The login method of discord.js is async, meaning that when you define that constant, the bot hasn't logged in yet. If the bot isn't logged in, it can't find any channels. Either keep the constant defined locally or make it a global let
and assign it when the ready event is emitted by the client object.
Upvotes: 1