【发布时间】:2020-09-17 00:03:29
【问题描述】:
起初,我将每个模块/命令编码在同一个文件中,最近我将它们分隔到 ./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
【问题讨论】:
标签: javascript node.js discord discord.js