Discord.js Discord bot 事件侦听器停止工作

时间:2021-05-08 17:41:36

标签: javascript node.js discord.js

我的一些不和谐机器人的事件侦听器由于某种原因停止工作。基本上我只有其中两个:'guildMemberAdd' 事件按预期工作,但 'message' 没有。 Bot 根本无法识别发送给它的命令。

我最接近的假设是我搞砸了同步/异步功能。

我的主要 bot.js 文件:

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 文件:

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);
        }
    }
};

1 个答案:

答案 0 :(得分:1)

我找到的解决方案是改变初始化机器人的方式。

替换这个:

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

有了这个:

const client = new Discord.Client();

这修复了代码。