[英]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.基本上我只有其中两个:'guildMemberAdd' 事件按预期工作,但 'message' 没有。 Bot simply doesn't recognise !commands sent to it. Bot 根本无法识别发送给它的命令。
My closest assumption is that i messed up sync / async functions.我最接近的假设是我搞砸了同步/异步功能。
My main bot.js file:我的主要 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 file:命令.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);
}
}
};
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.这修复了代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.