简体   繁体   English

Discord.js (V12) | 禁止命令

[英]Discord.js (V12) | Ban Command

I am making a discord bot and I want to make a ban command.我正在制作一个不和谐的机器人,我想发出一个禁令命令。 When I try to use the command though this error pops up in my terminal: TypeError: client.commands.get(...).execute is not a function at Client.<anonymous> (C:\Users\Lucas\Desktop\Discord BOT\index.js:29:32) What did I do wrong?当我尝试使用该命令时,虽然我的终端中弹出此错误: TypeError: client.commands.get(...).execute is not a function at Client.<anonymous> (C:\Users\Lucas\Desktop\Discord BOT\index.js:29:32)我做错了什么? I have tried multiple solutions I found on StackOverflow or v12.discordjs.guide But nothing I tried worked.我尝试了在 StackOverflow 或 v12.discordjs.guide 上找到的多种解决方案,但我没有尝试过。 Thanks for helping out!感谢您的帮助!

This is the index.js file:这是 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);

This is the ban command file (ban.js):这是禁令命令文件(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])

    }
}

In your index.js file, you run your commands by using client.commands.get(command).execute(message, args, Discord);在 index.js 文件中,使用client.commands.get(command).execute(message, args, Discord);运行命令

however, in your ban.js file, you have a run keyword not execute, try using that instead但是,在您的 ban.js 文件中,您有一个 run 关键字未执行,请尝试改用它

execute: async (message, client, args) => {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM