【问题标题】:Creating a menu like structure (discord.js)创建类似菜单的结构 (discord.js)
【发布时间】:2021-05-28 17:14:44
【问题描述】:

我想创建一个机器人可以执行的项目菜单,然后用户可以选择要执行的操作。

例如: 当我说+Menu 时,机器人会显示如下内容:

1. Time in NY
2. Movies currently running
3. Sports News

然后我想接受用户的输入(1,2 或 3),然后根据他们的选择,机器人将执行任务。 但是我不确定如何在命令(+Menu)之后阅读用户的输入并想寻求帮助。

【问题讨论】:

标签: javascript node.js discord.js


【解决方案1】:

您正在寻找message collector。请参阅文档here

我个人会创建一个嵌入,其中包含选项,例如

const menuEmbed = new Discord.MessageEmbed()
.setTitle("Menu")
.addFields(
  { name: "1.", value: "Time in NY"},
  { name: "2.", value:  "Movies currently running"},
  { name: "3.", value:  "Sports News"}
);

message.channel.send(menuEmbed).then(() => {
  const filter = (user) => {
    return user.author.id === message.author.id //only collects messages from the user who sent the command
  };
  
  try {
  let collected = await message.channel.awaitMessages(filter, { max: 1, time: 15000, errors: ['time'] });
      let choice = collected.first().content; //takes user input and saves it
  
      //do execution here
  }
  catch(e)
  {
    return message.channel.send(`:x: Setup cancelled - 0 messages were collected in the time limit, please try again`).then(m => m.delete({ timeout: 4000 }));
  };    
});   

然后使用收集器让用户选择一个选项。

请记住,这是使用 async/await 并且必须在 async 函数中。

【讨论】:

    猜你喜欢
    • 2014-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多