discord.js say command actually says “say”

人盡茶涼 提交于 2021-02-11 12:18:55

问题


I've never had a problem with the say command until I start using modules and cleaned up my code but since I have few my commands have bugged. I'm trying to figure out why it is saying the command.

const commandFiles = fs
  .readdirSync("./commands/")
  .filter(file => file.endsWith(".js"));
for (const file of commandFiles) {
  const command = require(`./commands/${file}`);

  bot.commands.set(command.name, command);
}
bot.on("message", async message => {
  if (!message.content.startsWith(PREFIX)) return;
  let args = message.content.substring(PREFIX.length).split(" ");

  switch (args[0]) {

    case "say":
      bot.commands.get("say").execute(message, args);
      break;
const Discord = require("discord.js");

module.exports = {
  name: "say",
  description: "Show's avatar",
 async execute(message, args){

    const sayMessage = args.join(" ");
    message.delete().catch(O_o => {});
    message.channel.send(`${sayMessage} - ${message.author}`);
  }
}

Thanks in advance!


回答1:


The code is doing exactly what you programmed it to, be it not intended.

let args = message.content.substring(PREFIX.length).split(" "); // args = ['say', 'Hello', 'World']

bot.commands.get("say").execute(message, args); // Passing in the entire args array

const sayMessage = args.join(" "); // sayMessage = 'say Hello World'

One solution of many:

let args = message.content.substring(PREFIX.length).split(" ");
const command = args.splice(0, 1); // args now only contains the arguments

switch (command) {
  ...
}



回答2:


Here you go this is using the anidiots guidebot boiler plate i highly recommend using it to help you better understand incorporating a command handler into your discord bot.

your going to want to download the repo from here https://github.com/AnIdiotsGuide/guidebot/

  1. extract it
  2. run npm install
  3. in the commands folder add say.js using the code below this is how to properly execute what your trying todo
  4. by default the prefix is ~ to change it in discord chat run ~conf edit prefix Example: ~conf edit prefix !
  5. run !say hello world
exports.run = async (client, message, args) => { 
    const sayMessage = args.join(" ");
    message.delete();
    message.channel.send(sayMessage + ' - ' + message.author)
  };

  exports.conf = {
    enabled: true,
    guildOnly: false,
    aliases: [],
    permLevel: "User"
  };

  exports.help = {
    name: "say",
    category: "Miscelaneous",
    description: "say command",
    usage: "say"
  };```



来源:https://stackoverflow.com/questions/59816652/discord-js-say-command-actually-says-say

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!