[英]Discord.js (V12) | Ban Command
我正在制作一個不和諧的機器人,我想發出一個禁令命令。 當我嘗試使用該命令時,雖然我的終端中彈出此錯誤: TypeError: client.commands.get(...).execute is not a function at Client.<anonymous> (C:\Users\Lucas\Desktop\Discord BOT\index.js:29:32)
我做錯了什么? 我嘗試了在 StackOverflow 或 v12.discordjs.guide 上找到的多種解決方案,但我沒有嘗試過。 感謝您的幫助!
這是 index.js 文件:
const fs = require('fs');
const Discord = require('Discord.js');
const { prefix, token } = require('./config.json');
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}`);
client.commands.set(command.name, command, Discord);
}
client.once('ready', () => {
console.log('Ready!');
});
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (!client.commands.has(command)) return;
try {
client.commands.get(command).execute(message, args, Discord);
} catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command!');
}
if (command.permissions) {
const authorPerms = message.channel.permissionsFor(message.author);
if (!authorPerms || !authorPerms.has(command.permissions)) {
return message.reply('You can not do this!');
}
}
});
client.login(token);
這是禁令命令文件(ban.js):
const Discord = require("Discord.js");
module.exports = {
name: 'ban',
description: 'Banish someone',
permissions: 'ADMINISTRATOR',
usage: 'ban @user <reason>',
run: async (message, client, args) => {
if (!target) {
return message.channel.send(`${message.author.username} USAGE: ban @user reason`)
}
if(target.id === message.author.id) {
return message.channel.send(`${message.author.username}, You can't ban yourself. YOU FOOL!`)
}
if(!args[1]) {
return message.channel.send(`${message.author.username}, Please give a reason to ban that member`)
}
let embed = new Discord.MessageEmbed()
.setTitle('Action: BAN')
.setDescription(`Banned ${target} (${target.id})`)
.setColor("0ff")
.setFooter(`Banned by ${message.author.tag}`);
message.channel.send(embed)
target.ban(args[1])
}
}
在 index.js 文件中,使用client.commands.get(command).execute(message, args, Discord);
運行命令
但是,在您的 ban.js 文件中,您有一個 run 關鍵字未執行,請嘗試改用它
execute: async (message, client, args) => {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.