javascript - Discord.js 如何制作建议命令?

标签 javascript node.js express

我可以在 Discord.js 中做什么来发出建议命令?我的实际代码不起作用,我有两种建议:支持服务器和机器人。 这是我的代码:

if (command === "suggest"){
    const type = args.join(" ")
    const thing = args.join(" ").slice(6)
    const thing2 = args.join(" ").slice(3)
    const suggestion = new Discord.RichEmbed()
       .setTitle("New suggestion!")
   .setAuthor(`${message.author.tag}`, `${message.author.avatarURL}`)
    .addField(`${message.author.tag}`, `suggested: ${thing}`)
    const suggestion2 = new Discord.RichEmbed()
       .setTitle("New suggestion!")
   .setAuthor(`${message.author.tag}`, `${message.author.avatarURL}`)
    .addField(`${message.author.tag}`, `suggested: ${thing2}`)
    if (!typ){
     message.reply("enter suggestion type, which you want to suggest!");
      return;
    }
            if (type === "server"){
               if (!thing){
     message.reply("enter thing you want to suggest!");
      return;
    }
      client.channels.get("channel_id").send(suggestion);
      return;
    }
    if (type === "bot"){
      if (!suggestion2){
     message.reply("enter thing you want to suggest!");
      return;
    }
      client.channels.get("another_channel_id").send(suggestion2);
      return;
    }
            else if (type !== "bot" || type !== "server"){
     message.reply("invalid suggestion type!"); 
    return;
        }
  }

当我输入 !suggestion 时,它会输出“invalid suggestion type!

最佳答案

我不是 discord.js 的专家,但这应该有用! (它在我的服务器上!)

client.on('message', msg => {    //This runs when a message is sent.
const args = msg.content.slice(prefix.length).split(' ');  //Get the arguments
const command = args.shift().toLowerCase();  //Making the command non case-sensitive


if (command === 'suggest'){   //if command is suggest
const channel = msg.guild.channels.find(ch => ch.name === 'suggestions');  //finds the channel named suggestions 

channel.send('Suggestion:\n ' + args.join(' '))  //Sends the arguments
}     //Closes the if (command === 'suggest'){ 
});   //Closes the client.on('message',msg => {

如果我能提供帮助,那就太好了!

您还使用了 !== 所以如果用户不是机器人它不会发送消息。尝试使用 === 代替!

关于javascript - Discord.js 如何制作建议命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54332216/

相关文章:

node.js - 很想知道使用基于 Express 渲染变量的 Handlebars 部分的最佳方法

javascript - 为什么 Router 在 express 4.x 中像普通函数一样使用而不是构造函数?

javascript - 减少数组以更改内容

node.js - 在 NestJS 中模拟多个 TypeORM 存储库

javascript - 无法为特定的 ul 提供不同的 css 样式?

node.js - bcrypt - Windows 10 x64 构建错误

node.js - 用于单元测试文件观察器库的模拟文件系统

javascript - 创建服务器时发送index.html文件

javascript - 傅里叶级数模拟器

javascript - window.pageYOffset 在 Chrome 中不起作用