我正试图为我的个人服务器编码一个不和机器人。我正在使用Discord.js,我一直在遵循Discord.js指南。
我现在有一个事件处理程序,但当我为另一个事件添加文件时,该模块的代码没有执行。我试图触发的事件是服务器中新成员的加入。
我有2个重要的文件:index.js它运行我的代码的尸体和guildMemberAdd.js这是我的事件模块,当一个新成员加入服务器。
index.js
:
// Require the necessary discord.js classes
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, GatewayIntentBits } = require('discord.js');
const { token } = require('./config.json');
// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));
for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
// Log in to Discord with your client's token
client.login(token);
guildMemberAdd.js
:
const { Events } = require('discord.js');
module.exports = {
name: Events.GuildMemberAdd,
async execute(member) {
console.log(member);
},
};
如果您只启用GatewayIntentBits.Guilds
意图,GuildMemberAdd
事件将不会触发。您还需要添加GatewayIntentBits.GuildMembers
(可能还有GatewayIntentBits.GuildPresences
):
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildPresences,
],
});
在discord.js v13中,它应该是:
const { Client, Intents } = require('discord.js');
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_PRESENCES,
],
});