[英]My Discord bot's mention help command is not working. Can someone help out? (Discord.js, JavaScript)
當提到我的機器人時,我希望它執行特定的命令。 但是,當它被提及時,它會引發一個錯誤,我將在我的代碼之后粘貼該錯誤。
PS:我使用命令處理程序。 PS 2:這個機器人托管在 repl.it
我的 index.js 代碼:
const fs = require('fs');
const Discord = require('discord.js');
const hosting = require('./hosting.js');
const client = new Discord.Client();
const token = process.env.botToken;
const {prefix} = require('./config.json');
module.exports = client
client.commands = new Discord.Collection();
const commandFolders = fs.readdirSync('./commands');
for (const folder of commandFolders) {
const commandFiles = fs.readdirSync(`./commands/${folder}`).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${folder}/${file}`);
client.commands.set(command.name, command);
}
}
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.once('ready', () => {
client.user.setPresence({activity: {name: '!1help', type: 'LISTENING'}, status: 'idle'});
console.log("Bot is running.");
console.log(client.user.username);
});
const escapeRegex = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
client.on('message', message => {
const prefixRegex = new RegExp(`^(<@!?${client.user.id}>|${escapeRegex(prefix)})\\s*`);
if (!prefixRegex.test(message.content)) return;
const [, matchedPrefix] = message.content.match(prefixRegex);
const args = message.content.slice(matchedPrefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
if (message.content === "<@833376214609690674>" || message.content === "<@!833376214609690674>") {
const mCommand = client.commands.get('help')
mCommand.execute()
}
if (!client.commands.has(commandName)) return;
const command = client.commands.get(commandName);
try {
command.execute(message, args);
} catch (error) {
console.error(error);
message.reply('There was an error trying to execute that command.');
}
})
client.login(token);
我的 help.js 代碼(這是我希望我的機器人在提到它時執行的命令):
const { prefix } = require('../../config.json');
module.exports = {
name: 'help',
category: "utility",
description: 'Lists all available commands or info about a specific command.',
usage: '[command name]',
execute(message, args) {
const data = [];
const { commands } = message.client
if (!args.length) {
data.push("Here's a list of all my commands:");
data.push(commands.map(command => command.name).join(', '));
data.push("\nYou can use `{prefix}help [command name]` to get info on a specific command!");
return message.author.send(data, { split: true })
.then(() => {
if (message.channel.type === 'dm') return;
message.reply("I've sent you a DM with all my commands!");
})
.catch(error => {
console.error(`Could not send help DM to ${message.author.tag}.\n`, error);
message.reply("It seems like I can't DM you! Do you have DMs disabled? If so, please enable them.");
});
}
const name = args[0].toLowerCase();
const command = commands.get(name) || commands.find(c => c.aliases && c.aliases.includes(name));
if (!command) {
return message.reply("That's not a valid command!");
}
data.push(`**Name:** ${command.name}`);
if (command.aliases) data.push(`**Aliases:** ${command.aliases.join(', ')}`);
if (command.category) data.push(`**category:** ${command.category}`)
if (command.description) data.push(`**Description:** ${command.description}`);
if (command.usage) data.push(`**Usage:** ${prefix}${command.name} ${command.usage}`);
if (command.cooldown === undefined) {
data.push(`**Cooldown:** 0 seconds`)
}
else if (command.cooldown === 1) {
data.push(`**Cooldown:** ${command.cooldown} second`)
}
else {
data.push(`**Cooldown:** ${command.cooldown} seconds`)
}
message.channel.send(data, { split: true });
},
};
help.js 文件的路徑:
./commands/utility/help.js
當 Discord 上提到機器人時,我在控制台中收到錯誤:
node v12.16.1
/home/runner/MultiBot/commands/utility/help.js:10
const { commands } = message.client
^
TypeError: Cannot read property 'client' of undefined
at Object.execute (/home/runner/MultiBot/commands/utility/help.js:10:30)
at Client.<anonymous> (/home/runner/MultiBot/index.js:60:12)
at Client.emit (events.js:314:20)
at Client.EventEmitter.emit (domain.js:483:12)
at MessageCreateAction.handle (/home/runner/MultiBot/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (/home/runner/MultiBot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (/home/runner/MultiBot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
at WebSocketShard.onPacket (/home/runner/MultiBot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
at WebSocketShard.onMessage (/home/runner/MultiBot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
at WebSocket.onMessage (/home/runner/MultiBot/node_modules/ws/lib/event-target.js:132:16)
execute(message, args) {
我需要在 index.js 中的 if function 中以這種方式傳遞執行和參數:
if (message.content === "<@833376214609690674>" || message.content === "<@!833376214609690674>") {
const mCommand = client.commands.get('help')
mCommand.execute(**message, args**)
}
比較您的命令處理程序和您自己的代碼以在提及時激活
你提到 function
mCommand.execute()
原始命令處理程序
command.execute(message, args);
如您所見,這兩者之間的區別是您的傳遞message, args
但mCommand.execute()
中沒有
由於您沒有傳遞任何參數,因此您無法從 help.js 中的help.js
獲得任何信息
但是您嘗試訪問它們execute(message, args) {
並且由於沒有傳遞任何message
,因此未定義(args 也未定義)
簡單的修復將在下面
mCommand.execute()
=> mCommand.execute(message, args)
ps:你應該學習javascript,它會幫助你的未來
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.