在“ message.channel.awaitmessages”行中,我认为代码写得不好,我想做的就是等待1条消息,即数字btw 0和23。
这是对应的代码:
client2.on("message", (message) => {
if(message.bot){return;} //if its a bot - disregard
if(message.content[0] !== prefix ){return;} //if its not a command by the user - disregard
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if (command === 'collect') {
const id = message.member.id;
const filter = m => m.content.endsWith('h');
message.channel.send(scheduler1).then(function(message) {
message.react('1️⃣')
.then(() => message.react('2️⃣'))
.then(() => message.react('3️⃣'))
.then(() => message.react('3️⃣'))
.then(() =>message.awaitReactions((reaction, user) =>(reaction.emoji.name == '1️⃣' || reaction.emoji.name == '2️⃣' || reaction.emoji.name == '3️⃣' || reaction.emoji.name == '4️⃣'),
{ max: 1, time: 30000 }).then(collected => {
if (collected.first().emoji.name == '1️⃣') {
message.edit(scheduler2);
message.clearReactions();
console.log("try");
message.channel.awaitMessages(filter, {max: 1, time: 6000, errors: ['time']})
.then(message.channel.send(collected))
.catch(collected => console.log(`After a minute, only ${collected.size} out of 4 voted.`));
}
if (collected.first().emoji.name == '2️⃣') {
message.edit(scheduler3);
message.clearReactions();
}
if (collected.first().emoji.name == '3️⃣') {
message.edit(scheduler4);
message.clearReactions();
}
}).catch(() => {
message.edit('No answer after 30 seconds, operation canceled.');
}));
})}
答案 0 :(得分:0)
您需要更新过滤器才能使其正常工作:
const filter = m => Number(m.content) >= 1 && Number(m.content) <= 23;
.then()另外还需要一个collected
。
message.channel.awaitMessages(filter, {max: 1, time: 6000, errors: ['time']})
.then(collected => message.channel.send(collected.first().content))
.catch(collected => console.log(`After a minute, only ${collected.size} out of 4 voted.`));
尝试async / await
也值得。 Wich将允许您执行以下操作:
client2.on('message', async (message) => {
if(message.bot) return; // if its a bot - disregard
if(message.content[0] !== prefix) return; // if its not a command by the user - disregard
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if (command === 'collect') {
const id = message.member.id;
const msg = await message.channel.send(scheduler1);
await msg.react('1️⃣');
await msg.react('2️⃣');
await msg.react('3️⃣');
await msg.react('3️⃣');
const filter = (reaction, user) => (reaction.emoji.name == '1️⃣' || reaction.emoji.name == '2️⃣' || reaction.emoji.name == '3️⃣' || reaction.emoji.name == '4️⃣');
const collected = await msg.awaitReactions(filter, { max: 1, time: 30000 })
.catch(() => {
msg.edit('No answer after 30 seconds, operation canceled.');
return;
});
if(!collected) return;
if (collected.first().emoji.name == '1️⃣') {
msg.edit(scheduler2);
msg.clearReactions();
console.log('try');
const filter2 = m => Number(m.content) >= 1 && Number(m.content) <= 23;
const collected2 = await msg.channel.awaitMessages(filter2, { max: 1, time: 6000, errors: ['time'] });
msg.channel.send(collected2.first().content)
.catch(collectedx => console.log(`After a minute, only ${collectedx.size} out of 4 voted.`));
}
if (collected.first().emoji.name == '2️⃣') {
msg.edit(scheduler3);
msg.clearReactions();
}
if (collected.first().emoji.name == '3️⃣') {
msg.edit(scheduler4);
msg.clearReactions();
}
}
});