首页 > 解决方案 > 消息和别名

问题描述

前几天我在我的机器人上开发,我想知道是否可以在消息上创建别名,例如我:“ping”,机器人:“Pong!”,所以我想做的是让我可以说:p机器人仍然会用 Pong 响应!我正在使用这段代码:

client.on('message', msg => {
    if (msg.content === 'ping') {
        msg.lineReplyNoMention('Pong!');
    } 
});

如果我可以在此代码上创建别名,请告诉我!

标签: javascriptnode.jsdiscorddiscord.js

解决方案


您可以创建一个别名列表并用于Array#includes()检查是否msg.content在该数组中:

let pingCommands = ['ping', 'p']
let msg = {}

msg.content = 'p'
if (pingCommands.includes(msg.content))
  console.log(msg.content, 'Pong!')

msg.content = 'pong'
if (pingCommands.includes(msg.content))
  console.log(msg.content, 'Pong!')

msg.content = 'ping'
if (pingCommands.includes(msg.content))
  console.log(msg.content, 'Pong!')

msg.content = 'penge'
if (pingCommands.includes(msg.content))
  console.log(msg.content, 'Pong!')

您的代码应如下所示:

client.on('message', msg => {
  let pingCommands = ['ping', 'p']
  if (pingCommands.includes(msg.content)) {
    msg.lineReplyNoMention('Pong!');
  } 
});

推荐阅读