如何使 discordjs 中的命令处理程序嵌入工作
2021-01-15
897
我成功地用这个 指南
但它怎么也不起作用 当我不嵌入时,它可以工作,但嵌入时不行 相反,当命令不起作用时,它会记录我添加的错误消息 这是我的代码
server-info.js
module.exports = {
name: 'server',
description: 'Display info about this server.',
execute(message) {
// message.channel.send(`Server name:${message.guild.name}\nTotal members: ${message.guild.memberCount}`);
const embed = new Discord.MessageEmbed()
.setTitle(`Server Info for Bot Test`)
message.channel.send(embed)
}
};
index.js
const fs = require('fs');
const Discord = require('discord.js')
const { prefix, token } = require('./setup.json');
const client = new Discord.Client();
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.on('ready', () => {
console.log("I have logged in!");
});
client.on('message', message =>{
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (!client.commands.has(command)) return;
try {
client.commands.get(command).execute(message, args);
} catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command!');
}
});
client.on('messageDelete', message => {
message.channel.send(`A message by ${message.author.tag} was deleted, but we don't know by who yet.`);
});
client.on('guildMemberAdd', member => {
const channel = member.guild.channels.cache.find(ch => ch.name === 'general');
if (!channel) return;
channel.send(`Welcome to the server, ${member}`);
});
client.login(token)
这是我得到的错误
ReferenceError: Discord is not defined
at Object.execute (/storage/emulated/0/hash/commands/server-info.js:6:23)
at Client.<anonymous> (/storage/emulated/0/hash/index.js:23:32)
at Client.emit (events.js:315:20)
at MessageCreateAction.handle (/storage/emulated/0/hash/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (/storage/emulated/0/hash/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (/storage/emulated/0/hash/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
at WebSocketShard.onPacket (/storage/emulated/0/hash/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
at WebSocketShard.onMessage (/storage/emulated/0/hash/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
at WebSocket.onMessage (/storage/emulated/0/hash/node_modules/ws/lib/event-target.js:132:16)
at WebSocket.emit (events.js:315:20)
有人可以帮忙解决这个问题吗? 我无法找到问题所在
2个回答
您需要将
const Discord = require('discord.js');
添加到您的命令文件:
const Discord = require('discord.js');
module.exports = {
name: 'server',
description: 'Display info about this server.',
execute(message) {
// message.channel.send(`Server name:${message.guild.name}\nTotal members: ${message.guild.memberCount}`);
const embed = new Discord.MessageEmbed()
.setTitle(`Server Info for Bot Test`)
message.channel.send(embed)
}
};
Jannik Schmidtke
2021-01-15
代码中确实有说明,“Discord”变量在 server-info.js 中未定义。您需要做的就是:
const Discord = require('discord.js');
将其放在 server-info.js 的开头
LouieBouie
2021-01-15