javascript - 为什么等待反应不起作用 Discord.js V12
问题描述
我试图通过等待该用户的反应来建立一个确认系统,由于某种原因,我无法让它工作。
这是代码:
if (command === 'reset') {
if (!msg.member.hasPermission('MANAGE_SERVER'))
msg.reply('You need `Mannage server` permission to delete the progress.');
//checking if author has mangage server permissions.
msg.channel
.send('Are you sure you want to delete all your progress?')
.then((message) => {
message.react('✅').then(() => message.react('❌'));
});
//confirming if author wants to delete channel.
const filter = (reaction, user) => {
return (
['✅', '❌'].includes(reaction.emoji.name) && user.id === msg.author.id
);
};
const fetchedChannel = msg.guild.channels.cache.find(
(channel) => channel.name === 'counting'
);
//getting the channel
msg
.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
.then((collected) => {
const reaction = collected.first();
if (reaction.emoji.name === '✅') {
fetchedChannel.delete();
msg.reply('Deleted all progress. to start over, run ".init"');
} else {
msg.reply('Aborting missing.');
return;
}
})
.catch((collected) => {
msg.reply('No response given.');
});
}
如果有人可以提供帮助,那就太好了!谢谢。
解决方案
我正在查看您的代码,我想我已经修复了它,因为我已经尝试过它并且它按预期工作。我所做的解释在代码中(第 19 行)。如果您对代码有任何疑问或仍有问题,我很乐意提供帮助。快乐编码
if (command === 'reset') {
if (!msg.member.hasPermission('MANAGE_SERVER'))
return msg.reply(
'You need `Mannage server` permission to delete the progress.'
); // You forgot to add a return to prevent the command from people without enough permissions
msg.channel
.send('Are you sure you want to delete all your progress?')
.then((message) => {
message.react('✅');
message.react('❌'); // I removed a .then(...)
//confirming if author wants to delete channel.
const filter = (reaction, user) => {
return (
['✅', '❌'].includes(reaction.emoji.name) && user.id === msg.author.id
);
};
const fetchedChannel = msg.guild.channels.cache.find(
(channel) => channel.name === 'counting'
);
//getting the channel
message
.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] }) // The problem was in this line, you used "msg" instead of "message", it means the bot wasn't awaiting reactions of its own message, it was awaiting reactions from the author's message.
.then((collected) => {
const reaction = collected.first();
if (reaction.emoji.name === '✅') {
fetchedChannel.delete();
msg.reply('Deleted all progress. to start over, run ".init"');
} else {
msg.reply('Aborting missing.');
return;
}
})
.catch((collected) => {
msg.reply('No response given.');
});
});
}
推荐阅读
- python - 在 Virtualenv 中的 Pip 安装工作,但 * 不 * 使用 `-r requirements.txt`
- google-apps-script - 使用可安装的 onedit 触发器从共享的谷歌表调用 showModalDialog
- android - 使用改造处理错误
- typescript - TS2554 预期 1 个参数,但 HTMLImageElement onerror() 为 0
- android - 如果没有 @Inject 构造函数或 @Provides-annotated 方法,则无法提供片段
- wpf - 在 DataTemplate 中绑定用户控件 Datacontext
- java - Java 数组列表参考
- qt - 如何使用 QML 显示通过网络传输的每一帧视频
- php - 如何添加我的功能并在控制器中正确使用它?
- java - 逐行从 Excel 工作表中读取嵌入对象