[英]Having trouble with Discord JS
At first, I was coding every module/command in the same file, recently I separated them to ./commands/(modules).js
and trying to export these commands to ./index.js
.起初,我在同一个文件中编写每个模块/命令,最近我将它们分离到
./commands/(modules).js
并尝试将这些命令导出到./index.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 (for example) 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);
})}}
The bot starts without any issues, but when I run the command !clear <any>
it giving me this error:机器人启动时没有任何问题,但是当我运行命令
!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)
Details: Using: Glitch (free) for host and coding Version: discord.js@11.6.4详细信息:使用:Glitch(免费)用于主机和编码版本:discord.js@11.6.4
command.execute(message, args);
you only run the function with 2 arguments, message and args.您只运行带有 2 个 arguments、消息和参数的 function。 However, you pass 3 arguments into the actual function (
execute(client, message, args) {
).但是,您将 3 arguments 传递给实际的 function (
execute(client, message, args) {
)。 So in this case, client
is a Message
, message
is an Array
and args
is undefined.所以在这种情况下,
client
是一个Message
, message
是一个Array
并且args
是未定义的。
One simple way of fixing this is by passing 2 arguments, the message and args, and removing the client.解决此问题的一种简单方法是传递 2 arguments、消息和参数,然后删除客户端。
So in your clear command file, it should be execute(message, args) {
instead of execute(client, message, args)
.因此,在您的明确命令文件中,它应该是
execute(message, args) {
而不是execute(client, message, args)
。
Hope i could help!希望我能帮上忙!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.