【问题标题】:I cannot add multiple prefixes in Discord.js我无法在 Discord.js 中添加多个前缀
【发布时间】:2021-02-10 18:57:49
【问题描述】:

当前前缀是'#'。

module.exports = async function(msg) {
    let args = msg.content.split(' ');
    let command = args.shift();
    if (command.charAt(0) === '#') {
      command = command.substring(1);
      commandName[command](msg, args);
    }
};

当我尝试为其添加一组前缀时,它似乎不起作用。

const prefix = [ '#', 'br', 'bread', '<@767558534074204191>', '<@!767558534074204191>', ];

module.exports = async function(msg) {
  let tokens = msg.content.split(' ');
  let command = tokens.shift();
  if (command.charAt(0) === prefix) {
    command = command.substring(1);
    commandName[command](msg, tokens);
  }
};

如何为此添加更多前缀,以便拥有多个前缀? 就像提到的机器人一样作为前缀之一。

【问题讨论】:

  • 抱歉,实际问题是什么? (请记住,标题是为了总结您的问题,而不是问题的一部分)
  • 如果你想要检查命令是否匹配任何指定的前缀,你可以使用正则表达式,例如:command.charAt(0).match(/^[#%]/)
  • 或者,如果您想将多个前缀都与它们自己的行为相匹配,switch-statement 或带有回调函数的查找映射可能会很有用。
  • 谢谢你,很抱歉没有总结问题 Mike Pomax 先生。我现在已经更新了。

标签: javascript node.js discord.js


【解决方案1】:

如果您有一个前缀数组,则无法将其与字符串的第一个字符进行比较。每次都是假的。您应该将字符串与字符串进行比较,因此您应该将数组的元素与消息进行比较。

您可以对数组使用.some() 方法来检查数组中是否至少有一个元素通过了提供的函数实现的测试。在该函数中,您可以检查消息 startsWith() 是否存在数组中的任何字符串。

如果messagearray 中的任何字符串开头,则类似array.some(el =&gt; message.startsWith(el)) 的函数会返回true

检查以下sn-p。您可以查看如何同时使用 .some() 和 .startsWith()

let message = '';
const prefixes = [ '#', 'br', 'bread', '<@767558534074204191>', '<@!767558534074204191>', ];

console.log(prefixes.some(prefix => message.startsWith(prefix)), message)

message = 'lorem ipsum'
console.log(prefixes.some(prefix => message.startsWith(prefix)), message)

message = '# lorem ipsum'
console.log(prefixes.some(prefix => message.startsWith(prefix)), message)

message = 'br lorem ipsum'
console.log(prefixes.some(prefix => message.startsWith(prefix)), message)

message = 'bread lorem ipsum'
console.log(prefixes.some(prefix => message.startsWith(prefix)), message)

message = '<@767558534074204191> lorem ipsum'
console.log(prefixes.some(prefix => message.startsWith(prefix)), message)

message = '<@!767558534074204191> lorem ipsum'
console.log(prefixes.some(prefix => message.startsWith(prefix)), message)

您的代码将如下所示:

const prefixes = [
  '#',
  'br',
  'bread',
  '<@767558534074204191>',
  '<@!767558534074204191>',
];

module.exports = async function(msg) {
  let tokens = msg.content.split(' ');
  let command = tokens.shift();

  if (prefixes.some(prefix => command.startsWith(prefix))) {
    command = command.substring(1);
    commandName[command](msg, tokens);
  }
};

更新:我刚刚检查并注意到您尝试使用 command.substring(1) 从命令中删除前缀。硬编码的1 将始终只删除第一个字符,因此您需要将其更改为前缀的长度。

目前,使用.some() 您不知道使用哪个前缀,因此最好使用.find() 方法,该方法返回提供的数组中满足提供的测试功能的第一个元素的值。这样,您可以检查消息是否包含任何前缀,并且您知道前缀及其长度。运行以下sn -p 是如何工作的:

// I changed the order of prefixes as bread starts with br
const prefixes = [ '#', 'bread', 'br', '<@767558534074204191>', '<@!767558534074204191>', ];
let prefix = '';
let command = '';

command = 'lorem ipsum'
prefix = prefixes.find((prefix) => command.startsWith(prefix));
console.log({command, prefix});

command = '# lorem ipsum'
prefix = prefixes.find((prefix) => command.startsWith(prefix));
console.log({command, prefix});

command = 'br lorem ipsum'
prefix = prefixes.find((prefix) => command.startsWith(prefix));
console.log({command, prefix});

command = 'bread lorem ipsum'
prefix = prefixes.find((prefix) => command.startsWith(prefix));
console.log({command, prefix});

command = '<@767558534074204191> lorem ipsum'
prefix = prefixes.find((prefix) => command.startsWith(prefix));
console.log({command, prefix});

command = '<@!767558534074204191> lorem ipsum'
prefix = prefixes.find((prefix) => command.startsWith(prefix));
console.log({command, prefix});

所以您修改后的代码将如下所示:

client.on('message', (msg) => {
  let tokens = msg.content.split(' ');
  let command = tokens.shift();
  let prefix = prefixes.find((prefix) => command.startsWith(prefix));

  if (prefix) {
    command = command.substring(prefix.length);
    commandName[command](msg, tokens);
  }
});

【讨论】:

    【解决方案2】:

    线条

    const prefix = [ '#', 'br', 'bread', '<@767558534074204191>', '<@!767558534074204191>' ];
    
      if (command.charAt(0) === prefix) {
         ... do stuff
      }
    };
    

    您定义了一个包含 5 个值的数组,然后您尝试查看字符串命令的第一个字符是否等于该数组。这永远不会评估为 true,因为函数 .charAt(0) 将只返回该字符串的第一个字符。

    您要做的是根据命令检查数组的每个值。我们可以在遍历数组时对字符串使用startsWith() 函数。

    类似:

    const prefix = [ '#', 'br', 'bread', '<@767558534074204191>', '<@!767558534074204191>' ];
      
      // see if a value in the prefix array is at the beginning of the command.
      for(let x = 0; x < prefix.length; x++) {
          if (command.startsWith(prefix[x])) { 
              ...do whatever you were intending to here
              break // break out of the loop by using the break keyword.         
          }
      }
      })
    };
    

    【讨论】:

      猜你喜欢
      • 2020-12-26
      • 2021-04-07
      • 2021-04-08
      • 2021-04-09
      • 2023-03-28
      • 2021-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多