[英]I'm coding a discord bot using discord.js and I'm working on a suggestion command
I'm currently trying to get the bot to send a filler message to the channel I eventually want the suggestions to go to and have tried multiple methods to do so, but keep getting errors.我目前正在尝试让机器人向频道发送填充消息,我最终希望向 go 发送建议,并尝试了多种方法,但不断出现错误。 I'm sure the solution is very simple, and any help is much appreciated.
我确信解决方案非常简单,非常感谢任何帮助。
suggest.js建议.js
module.exports = {
name: 'suggest',
aliases: ['suggestion'],
description: 'Sends a suggestion to the <#700591796119535657> channel.',
usage: '<your suggestion>',
cooldown: 1,
execute(message, args) {
const Discord = require('discord.js');
const client = new Discord.Client();
const channel = client.channels.cache.get('701087240729657457');
channel.send('test');
},
};
error message in console upon running.suggest运行时控制台中的错误消息。建议
TypeError: Cannot read property 'send' of undefined
at Object.execute (/home/shares/public/RetroCraft/commands/suggest.js:11:11)
at Client.<anonymous> (/home/shares/public/RetroCraft/index.js:69:11)
at Client.emit (events.js:310:20)
at MessageCreateAction.handle (/home/shares/public/RetroCraft/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (/home/shares/public/RetroCraft/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (/home/shares/public/RetroCraft/node_modules/discord.js/src/client/websocket/WebSocketManager.js:386:31)
at WebSocketShard.onPacket (/home/shares/public/RetroCraft/node_modules/discord.js/src/client/websocket/WebSocketShard.js:436:22)
at WebSocketShard.onMessage (/home/shares/public/RetroCraft/node_modules/discord.js/src/client/websocket/WebSocketShard.js:293:10)
at WebSocket.onMessage (/home/shares/public/RetroCraft/node_modules/ws/lib/event-target.js:120:16)
at WebSocket.emit (events.js:310:20)
index.js索引.js
const fs = require('fs');
const Discord = require('discord.js');
const { prefix, token } = require('./config.json');
const cooldowns = new Discord.Collection();
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}`);
// set a new item in the Collection
// with the key as the command name and the value as the exported module
client.commands.set(command.name, command);
}
client.once('ready', () => {
console.log('Ready!');
client.user.setActivity('RetroCraft', { type: 'WATCHING' });
});
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName)
|| client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
if (!command) return;
if (command.guildOnly && message.channel.type !== 'text') {
return message.reply('I can\'t execute that command inside DMs!');
}
if (command.args && !args.length) {
let reply = `You didn't provide any arguments, ${message.author}!`;
if (command.usage) {
reply += `\nThe proper usage would be: \`${prefix}${command.name} ${command.usage}\``;
}
return message.channel.send(reply);
}
if (!cooldowns.has(command.name)) {
cooldowns.set(command.name, new Discord.Collection());
}
const now = Date.now();
const timestamps = cooldowns.get(command.name);
const cooldownAmount = (command.cooldown || 3) * 1000;
if (timestamps.has(message.author.id)) {
const expirationTime = timestamps.get(message.author.id) + cooldownAmount;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
return message.reply(`please wait ${timeLeft.toFixed(1)} more second(s) before reusing the \`${command.name}\` command.`);
}
}
timestamps.set(message.author.id, now);
setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);
try {
command.execute(message, args);
}
catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command!');
}
});
client.login(token);
This is a really easy fix.这是一个非常简单的修复。 Just change the
channel.send
to message.channel.send
and that should work.只需将
channel.send
更改为message.channel.send
即可。 Tell me if you get any errors and I'll try to help:)如果您遇到任何错误,请告诉我,我会尽力提供帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.