【问题标题】:Discord.js command handler problemsDiscord.js 命令处理程序问题
【发布时间】:2020-06-09 17:16:02
【问题描述】:

我已经尝试修复命令处理程序 3 个小时了,但无论何时我尝试添加自定义命令,都没有任何反应。它加载命令,但是当命令运行时它什么也不做。这是一些代码:

index.js:

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    console.log(`[LOG] Loaded command ${file}`);
    client.commands.set(command.name, command);
}

命令/kick.js:

const fs = require('fs');
var moment = require('moment');
var logger = fs.createWriteStream(`./logs/${moment().format('MM-DD-YYYY')}.log`, {
    flags: 'a'
  });

module.exports = {
    name: "kick",
    category: "moderation",
    description: "Kicks the mentioned user.",
    usage: "<imputs>",
    run: (client, message, args) => {
    let reason = args.slice(1).join(' ');
    let user = message.mentions.users.first();
    if (message.mentions.users.size < 1) return message.reply('You must mention someone to kick them.').then(msg => { msg.delete(10000) }).catch(console.error);
    if (user.id === message.author.id) return message.reply("You cannot kick yourself.").then(msg => { msg.delete(10000) });
    if (user.id === client.user.id) return message.reply("You cannot kick me.").then(msg => { msg.delete(10000) });

    if (!message.member.hasPermission("KICK_MEMBERS")) return message.reply("You don't have the **Kick Members** permission!").then(msg => { msg.delete(10000) });

    if (reason.length < 1) reason = 'No reason supplied';

    if (!message.guild.member(user).kickable) return message.reply('I could not kick that member').then(msg => { msg.delete(10000) });

    message.delete();
    message.guild.member(user).kick();

    const embed = new Discord.RichEmbed()
      .setColor(0x0000FF)
      .setTimestamp()
      .addField('Action:', 'Kick')
      .addField('User:', `${user.username}#${user.discriminator} (${user.id})`)
      .addField('Moderator:', `${message.author.username}#${message.author.discriminator}`)
      .addField('Reason', reason)
      .setFooter(`© NetSync by Towncraft Developers`);
    let logchannel = message.guild.channels.find('name', 'logs');
    if  (!logchannel){
      message.channel.send(`Successfully kicked ${user.username}#${user.discriminator}.`).then(msg => { msg.delete(10000) });
      logger.log(`[LOG] [KICK] ${user.username}#${user.discriminator} (${user.id}) was kicked by ${message.author.username}#${message.author.discriminator}.`);
    }else{
      message.channel.send(`Successfully kicked ${user.username}#${user.discriminator}. I\'ve also logged the kick in <#${logchannel.id}>.`).then(msg => { msg.delete(10000) });
      client.channels.get(logchannel.id).send({embed});
    }
    if(user.bot) return;
    return message.mentions.users.first().send({embed}).catch(e =>{
      if(e) return
    });
  }
}

就像我说的那样,我已经尝试了 3 个小时,但我很难过。如果有人能告诉我我做错了什么,那就太好了,谢谢。

【问题讨论】:

  • 我没有看到你正在执行命令的 run 方法的任何地方。你处理过消息事件吗?
  • @Tarazed 我该怎么做?

标签: discord.js


【解决方案1】:

您需要在 bot.on('message' 块上的 main.js 文件中执行命令。

像这样:

client.on('message', message => {
    if (message.channel.type === "dm") return;

    let prefix = '!'
    let messageArray = message.content.split(" ");
    let cmd = messageArray[0];
    let args = messageArray.slice(1);
    if (!message.content.startsWith(prefix)) return;

    let commandfile = client.commands.get(cmd.slice(prefix.length));
    if (commandfile) commandfile.run(client, message, args, botconfig);
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 2021-11-02
    • 2021-08-01
    • 2020-06-17
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多