【问题标题】:How to make my Discord bot to answer my questions with a random answer?如何让我的 Discord 机器人随机回答我的问题?
【发布时间】:2021-02-01 23:18:40
【问题描述】:

我尝试制作一个 Discord 机器人,它可以从一组回复中随机选择一个回复。如果问题未知或命令后没有参数,我还希望它发送错误消息。

我有以下代码,但它不起作用。它确实得到一个随机回复,但它无法通过收到的参数 (args[0]) 找到问题。

module.exports.run = async(client, message, args, bot, sendMessage) => {
  var array1 = ["reply1"];
  var rand = Math.floor(Math.random() * array1.length);

  const pergunta = parseInt(args[0], 10);
  if (!pergunta)
    return message.reply("make your question");

  message.channel.send("" + array1[rand] + "");
}

【问题讨论】:

  • 你到底想做什么?
  • 我正在尝试使用来自 loritta bot 的 vieirinha 之类的 make 命令
  • 命令前面的字符一次验证
  • 喜欢 command=error & command+any character = 回复
  • 以易于理解的方式为我们提供几个示例可能是个好主意。比如“我希望我的机器人用别的东西来响应命令!cmd something

标签: javascript node.js discord.js


【解决方案1】:

您可以编写一个从数组中选择随机元素的命令。

您可以将问题存储在对象中。这样,您可以轻松检查(和访问)用户提交的问题是否有效。您可以为每个问题添加一系列答案,然后选择其中一个:

function pickOne(arr) {
  return arr[Math.floor(Math.random() * arr.length)];
}

const questions = {
  question1: {
    text: 'This is question one',
    replies: ['reply 1', 'reply 2', 'reply 3', 'reply 4', 'reply 5'],
  },
  question2: {
    text: 'This is question two',
    replies: ['reply 11', 'reply 12', 'reply 13', 'reply 14', 'reply 15'],
  },
};

module.exports.run = async (client, message, args, bot, sendMessage) => {
  if (args.length === 0) {
    return message.reply('Oops, forgot your question?!');
  }

  const question = questions[args[0]];

  if (!question) {
    return message.reply("It seems that's not a question I can answer ?");
  }

  const reply = pickOne(question.replies);
  return message.reply(reply);
};

结果:

【讨论】:

  • 是的,这是可行的,但我需要验证命令前面是否存在某些字符,他的回复来自数组的一些回复,如果不存在,则返回 you not make a question
  • 再举个例子就好了。
  • 点赞 !cmd = 不回复或返回错误消息 !cmd question 从数组返回随机回复
  • 好的,我刚刚更新了我的答案。检查这是否是您想要的。
  • 非常感谢你,这正是我想做的事
猜你喜欢
  • 2020-11-08
  • 1970-01-01
  • 2021-11-15
  • 2021-03-22
  • 2022-01-25
  • 2021-04-09
  • 2021-04-03
  • 2021-06-13
  • 1970-01-01
相关资源
最近更新 更多