首先,我在同一个文件中对每个模块/命令进行编码,最近我将它们分离到./commands/(modules).js
并尝试将这些命令导出到./index.js
。
index.js:
const Discord = require("discord.js");
const client = new Discord.Client({disableEveryone: true});
const config = require("./config.json");
const fs = require("fs");
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("message", async message => {
if(message.author.bot) return;
if(message.content.indexOf(config.prefix) !== 0) return;
const time = new Date()
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const commandName = args.shift().toLowerCase();
if (!client.commands.has(commandName)) return;
const command = client.commands.get(commandName);
if (command.guildOnly && message.channel.type !== 'text') {
return message.reply('Bu komutu özel mesaj aracılığıyla kullanamazsın!');
}
if (command.args && !args.length) {
let reply = `Eksik komut girdiniz, ${message.author}!`;
if (command.usage) {
reply += `\nKullanım \`${config.prefix}${command.name} ${command.usage}\``;
}
return message.channel.send(reply);
}
try {
command.execute(message, args);
} catch (error) {
console.error(error);
message.reply('komutu girerken bir hata oluştu!');
});
commands / clear.js(例如)
const Discord = require("discord.js");
module.exports = {
name: "clear",
usage: "<sayı>",
guildOnly: true,
description: "Sohbeti temizlemek için bir komut",
execute(client, message, args) {
if (message.deleteable) {
message.delete();
}
if (!message.member.hasPermission("MANAGE_MESSAGES")) return message.reply("Bu komutu kullanmak için yeterli yetkiniz yok.").then(message => message.delete(5000));
if (isNaN(args[0]) || parseInt(args[0]) <- 0) return message.reply("Bir sayı girmeniz gerekli").then(message => message.delete(5000));
if (!message.guild.me.hasPermission("MANAGE_MESSAGES")) return message.reply("Üzgünüm, bunu yapmak için yeterli yetkim yok.").then(message => message.delete(5000));
let deleteAmount;
if (parseInt(args[0]) > 100) {
deleteAmount = 100;
} else {
deleteAmount = parseInt(args[0]);
}
message.channel.bulkDelete(deleteAmount, true)
.then(async (deleted) => {
const deleteMessage = await message.channel.send(`:white_check_mark: **${deleted.size}** adet mesaj silindi.`);
await setTimeout(() => deleteMessage.delete(), 5 * 1000);
})}}
该机器人启动时没有任何问题,但是当我运行命令!clear <any>
时,它给了我这个错误:
TypeError: Cannot read property 'hasPermission' of undefined
at Object.execute (/app/commands/clear.js:12:25)
at Client.client.on (/app/index.js:86:13)
at Client.emit (events.js:189:13)
at MessageCreateHandler.handle (/rbd/pnpm-volume/daf7c446-62cc-4e90-99e4-775279447b67/node_modules/discord.js/src/client/websocket/packets/handlers/MessageCreate.js:9:34)
at WebSocketPacketManager.handle (/rbd/pnpm-volume/daf7c446-62cc-4e90-99e4-775279447b67/node_modules/discord.js/src/client/websocket/packets/WebSocketPacketManager.js:108:65)
at WebSocketConnection.onPacket (/rbd/pnpm-volume/daf7c446-62cc-4e90-99e4-775279447b67/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:336:35)
at WebSocketConnection.onMessage (/rbd/pnpm-volume/daf7c446-62cc-4e90-99e4-775279447b67/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:299:17)
at WebSocket.onMessage (/rbd/pnpm-volume/daf7c446-62cc-4e90-99e4-775279447b67/node_modules/ws/lib/event-target.js:120:16)
at WebSocket.emit (events.js:189:13)
at Receiver.receiverOnMessage (/rbd/pnpm-volume/daf7c446-62cc-4e90-99e4-775279447b67/node_modules/ws/lib/websocket.js:789:20)
详细信息: 使用:小故障(免费),用于主机和编码 版本:discord.js@11.6.4
答案 0 :(得分:0)
command.execute(message, args);
仅使用2个参数message和args运行该函数。但是,您将3个参数传递给实际函数(execute(client, message, args) {
)。因此,在这种情况下,client
是Message
,message
是Array
,而args
是未定义的。
解决此问题的一种简单方法是传递2个参数(消息和args),然后删除客户端。
因此,在您清晰的命令文件中,它应该是execute(message, args) {
而不是execute(client, message, args)
。
希望我能帮上忙!