【问题标题】:Arguments for Discord.jsDiscord.js 的参数
【发布时间】:2019-09-04 07:55:33
【问题描述】:

如何在 discord.js 中读取 args?我正在尝试创建一个支持机器人,并且我想要一个 !help {topic} 命令。我怎么做? 我当前的代码非常基础

const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = ("!")
const token = ("removed")

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message', msg => {
  if (msg.content === 'ping') {
    msg.reply('pong');
  }
  if (msg.content === 'help') {
    msg.reply('type -new to create a support ticket');
  }

});

client.login(token);

【问题讨论】:

    标签: discord.js


    【解决方案1】:

    您可以像这样使用前缀和参数...

    const prefix = '!'; // just an example, change to whatever you want
    
    client.on('message', message => {
      if (!message.content.startsWith(prefix)) return;
    
      const args = message.content.trim().split(/ +/g);
      const cmd = args[0].slice(prefix.length).toLowerCase(); // case INsensitive, without prefix
    
      if (cmd === 'ping') message.reply('pong');
    
      if (cmd === 'help') {
        if (!args[1]) return message.reply('Please specify a topic.');
        if (args[2]) return message.reply('Too many arguments.');
    
        // command code
      }
    });
    

    【讨论】:

    • 一旦你开始有更多的命令,你最好使用switch 语句而不是if 语句来检查命令。或者,您现在可以开始一个更好的选择,使用命令处理程序。 :)
    【解决方案2】:

    您可以使用 Switch 语句代替 if (command == 'help') {} else if (command == 'ping') {}

    client.on ('message', async message => {
      var prefix = "!";
      var command = message.content.slice (prefix.length).split (" ")[0],
          topic = message.content.split (" ")[1];
      switch (command) {
        case "help":
          if (!topic) return message.channel.send ('no topic bro');
          break;
        case "ping":
          message.channel.send ('pong!');
          break;
      }
    });
    

    【讨论】:

      【解决方案3】:
      let args = msg.content.split(' ');
      let command = args.shift().toLowerCase();
      

      这是来自@slothiful 的简化答案。

      用法

      if(command == 'example'){
        if(args[0] == '1'){
        console.log('1');
       } else {
        console.log('2');
      

      【讨论】:

        【解决方案4】:

        你可以创建一个简单的命令/参数的东西(我不知道如何正确地用词)

        client.on("message", message => {
          let msgArray = message.content.split(" "); // Splits the message content with space as a delimiter
          let prefix = "your prefix here";
          let command = msgArray[0].replace(prefix, ""); // Gets the first element of msgArray and removes the prefix
          let args = msgArray.slice(1); // Remove the first element of msgArray/command and this basically returns the arguments
        
          // Now here is where you can create your commands
          if(command === "help") {
            if(!args[0]) return message.channel.send("Please specify a topic.");
            if(args[1]) return message.channel.send("Too many arguments.");
        
            // do your other help command stuff...
          }
        });
        

        【讨论】:

          【解决方案5】:

          你可以的

                      const args =
                          message.content.slice(prefix.length).trim().split(' ');
                      const cmd = args.shift().toLocaleLowerCase();
          

          【讨论】:

            猜你喜欢
            • 2021-04-13
            • 1970-01-01
            • 2021-07-14
            • 2018-08-07
            • 2021-04-19
            • 2021-04-26
            • 2020-10-11
            • 1970-01-01
            • 2021-07-17
            相关资源
            最近更新 更多