【问题标题】:discord.js say command actually says "say"discord.js 说命令实际上说“说”
【发布时间】:2020-01-20 19:17:48
【问题描述】:

在我开始使用模块并清理我的代码之前,我从来没有遇到过 say 命令的问题,但是因为我的命令很少有问题。我试图弄清楚为什么它会说这个命令。

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}`);
  }
}

提前致谢!

【问题讨论】:

    标签: discord discord.js


    【解决方案1】:

    代码完全按照您的编程方式执行,尽管它不是预期的。

    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'
    

    一个解决方案:

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

    【讨论】:

    • 感谢大家的帮助:)
    【解决方案2】:

    这里是使用 anidiots guidebot 样板,我强烈建议使用它来帮助您更好地了解将命令处理程序合并到您的不和谐机器人中。

    你会想从这里下载 repo https://github.com/AnIdiotsGuide/guidebot/

    1. 解压
    2. 运行 npm 安装
    3. 在命令文件夹中添加 say.js 使用下面的代码这是如何正确执行您尝试做的事情
    4. 默认前缀是~在discord聊天运行中更改它~conf编辑前缀示例:~conf编辑前缀!
    5. 运行 !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"
      };```
    
    

    【讨论】:

    • const args = message.content.slice(settings.prefix.length).trim().split(/ +/g); const command = args.shift().toLowerCase();
    • 这是解决您问题的最常见方法。您可以将第一个参数移出数组,在您的情况下将始终是命令名称。 const args = message.content.slice(settings.prefix.length).trim().split(/ +/g); const command = args.shift().toLowerCase();
    猜你喜欢
    • 2021-12-20
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-15
    相关资源
    最近更新 更多