我对任何编程都不熟悉。在我的Discord机器人中,主文件查找要执行命令的模块。
# print(df)
Rain Running
0 0.0 0.0
1 0.8 0.8
2 2.4 3.2
3 19.4 22.6
4 6.2 28.8
5 1.0 29.8
6 0.0 0.0
其中一个命令应该在触发后将消息发送到某个通道:
const Discord = require('discord.js');
require('dotenv').config();
const bot = new Discord.Client();
bot.commands = new Discord.Collection();
const botCommands = require('./commands');
Object.keys(botCommands).map(key => {
bot.commands.set(botCommands[key].name, botCommands[key]);
});
const TOKEN = process.env.TOKEN;
bot.login(TOKEN);
bot.on('ready', () => {
console.info(`Logged in as ${bot.user.tag}!`);
});
bot.on('message', msg => {
const args = msg.content.split(/ +/);
const command = args.shift().toLowerCase();
console.info(`Called command: ${command}`);
if (!bot.commands.has(command)) return;
try {
bot.commands.get(command).execute(msg, args);
} catch (error) {
console.error(error);
msg.reply('Check it out! You *failed*.');
}
});
但它会出现以下错误:
module.exports ={
name: '!start',
description: 'Starts the killing game',
execute(msg, args) {
client.on('ready', client => {
client.channels.cache.get('724557257484009516').send('The killing game is about to start!');
});
},
};
我该如何解决?
答案 0 :(得分:1)
您没有将客户端变量从index.js文件传递到命令文件,也不要将事件侦听器放入命令中,这是一个很糟糕的主意,而是执行以下操作:
execute(msg, args) {
Let guild = msg.client.guilds.cache.get(‘ID of the guild in which the channel is in);
guild.channels.cache.get('724557257484009516').send('The killing game is about to start!');
},
我使用msg.client
是因为,来自discord.js documentation
:message.client
实例化此消息的客户端,因此是您的漫游器。