我想创建一个机器人可以执行的项目菜单,然后用户可以选择要执行的操作。
例如:当我说+Menu然后机器人会显示如下内容:
1. Time in NY
2. Movies currently running
3. Sports News然后我想接受用户的输入(1,2或3),然后根据他们的选择,机器人将执行任务。但我不确定如何阅读命令(+菜单)后用户的输入,并想寻求帮助。
发布于 2021-02-26 18:23:20
您正在寻找一个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函数。
https://stackoverflow.com/questions/66383858
复制相似问题