【问题标题】:Discord.js Discord bot event listeners stopped workingDiscord.js Discord 机器人事件监听器停止工作
【发布时间】:2021-07-30 16:15:03
【问题描述】:

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

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

我的主要 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);
        }
    }
};

【问题讨论】:

  • 您也应该向我们展示您的commands.js 文件的内容。
  • @ZsoltMeszaros。你在这。我更新了问题
  • 您正在向机器人发送类似!help 之类的命令?
  • @LoganDevine 是的。我希望它处理 DM 命令
  • 是否有任何日志记录到控制台?您是否尝试过注释掉这一行:commands[command](msg, tokens); 并将控制台日志放在那里”您是否尝试过在运行程序时使用 VSCode 的调试器?

标签: javascript node.js discord.js


【解决方案1】:

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

替换这个:

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

有了这个:

const client = new Discord.Client();

这修复了代码。

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 2021-09-04
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 2016-01-07
    • 2021-07-01
    • 1970-01-01
    • 2016-04-29
    相关资源
    最近更新 更多