简体   繁体   中英

Discord.js Discord bot event listeners stopped working

Some of my discord bot's event listeners stopped working for some reason. Basically i have only two of them: 'guildMemberAdd' event works as intended but 'message' doesn't. Bot simply doesn't recognise !commands sent to it.

My closest assumption is that i messed up sync / async functions.

My main bot.js file:

console.log('Beep Boop Beep'); //prints 'Beep Boop Beep'

require('dotenv').config(); //load the dotenv node pachage and call a config() func to load thea values from .env

const Discord = require('discord.js');

const client = new Discord.Client({ ws: { intents: [
    'GUILDS',
    'GUILD_MESSAGES',
    'GUILD_PRESENCES',
    'GUILD_MEMBERS'
] } });

//this line authenticates the bot with the discord API
client.login(process.env.BOTTOKEN); 

const embed = new Discord.MessageEmbed()
    .setColor('#0099ff')
    .setTitle('Some title')
    .setURL('https://discord.js.org/')
    .setAuthor('Some name', 'https://i.imgur.com/wSTFkRM.png', 'https://discord.js.org')
    .setDescription('Some description here')
    .setThumbnail('https://i.imgur.com/wSTFkRM.png')
    .addFields(
        { name: 'Regular field title', value: 'Some value here' },
        { name: '\u200B', value: '\u200B' },
        { name: 'Inline field title', value: 'Some value here', inline: true },
        { name: 'Inline field title', value: 'Some value here', inline: true },
    )
    .addField('Inline field title', 'Some value here', true)
    .setImage('https://i.imgur.com/wSTFkRM.png')
    .setTimestamp()
    .setFooter('Some footer text here', 'https://i.imgur.com/wSTFkRM.png');

//exporting those so that they are available in other .js files
module.exports = {client, embed};

function readyDiscord() {
    console.log('readyDiscord func executed. Discord is ready.'); 
}

client.on('ready', readyDiscord); 

const commandHandler = require('./commands'); 
client.on('message', commandHandler);

client.on('guildMemberAdd', async member => { 
    console.log(member)

    const message = `Hey, <@${member.id}>! Welcome. DM me if anytime you want.`
    
    //const channel = member.guild.channels.cache.get(process.env.WELCOME_CHANNEL_ID)
    const channel = await member.guild.channels.resolve(process.env.WELCOME_CHANNEL_ID);
    channel.send(message)
})

commands.js file:

const hey = require('./commands/hey.js');
const bind = require("./commands/bind.js");
const help = require("./commands/help.js");

const commands = { bind, hey, help };

module.exports = function (msg) {

   
    if (msg.channel.type === 'dm') {    
        let tokens = msg.content.split(" ");
        let command = tokens.shift();

        //bot command is only valid when it starts with a !
        if (command.charAt(0) === '!') {
            //this one removes the !
            command = command.substring(1);

            commands[command](msg, tokens);
        }
    }
};

The solution I found was the change up the way you initialize your bot.

Replace this:

const client = new Discord.Client({ ws: { intents: [
    'GUILDS',
    'GUILD_MESSAGES',
    'GUILD_PRESENCES',
    'GUILD_MEMBERS'
] } });

With this:

const client = new Discord.Client();

This fixes the code up.

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