为报告不一致机器人投票

时间:2020-05-28 15:30:54

标签: javascript bots discord discord.js

我正在尝试制作一个机器人,人们可以调用该机器人进行报告投票,如果投票成功,并且赞成票数大于2(或X数)。它会将消息发送到一个单独的通道,只有该机器人可以在其中发布消息,并将DM发送到服务器管理员(这是我不知道该怎么做)。

我也收到一条错误消息:

"const sentEmbed = await msg.channel.send(voting); // Send Embed
                   ^^^^^
SyntaxError: await is only valid in async function"

这是我到目前为止的代码:

if(!msg.mentions.users.first()) return msg.channel.send('You need to mention somebody!'); // Check if no User was Mentioned
const voting = async function new Discord.RichEmbed() // Generate Voting Embed
    .setColor('#42b34d')
    .setFooter('Mute ' + msg.mentions.users.first().tag + ' for 10m?')
    .setImage(msg.mentions.users.first().avatarURL);
const role = msg.guild.roles.find(r => r.name === 'Muted'); // Find Role
if(!role) return msg.channel.send('No Role was found, please make sure you have a muteed role!'); // Make sure there is a Role
const agree = '✅'; // Define Emojis
const disagree = '❌'; // Define Emojis

const sentEmbed = await msg.channel.send(voting); // Send Embed
const filter = (reaction, user) => (reaction.emoji.name === agree || reaction.emoji.name === disagree) && !user.bot; // Filter for Reactions
await sentEmbed.react(agree); // React
await sentEmbed.react(disagree); // React
const voteStatus = await msg.channel.send('Voting started 30 seconds left'); // Send start Message and
const collected = await sentEmbed.awaitReactions(filter, { time: 5000 }); // start Collecting Reactions
const agreed = collected.get(agree) || { count: 1 }; // Retrieve Reactions
const disagreed = collected.get(disagree) || { count : 1 }; // Retrieve Reactions
const agreed_count = agreed.count - 1 ; // Count away Bot Votes
const disagreed_count = disagreed.count - 1; // Count away Bot Votes
voteStatus.edit('Voting endet with: ' + agreed_count + agree + ' and ' + disagreed_count + disagree); // Edit message to show Outcome
if(agreed.count > disagreed.count) {

}
else {
    msg.channel.send('Mute Voting Failed :)');
}

我从另一篇文章中获得了这段代码。我当时试图适应它,但失败了。我是Java的新手。

1 个答案:

答案 0 :(得分:0)

该错误的原因是您在异步函数之外调用await

异步功能示例;

async function myFunction() {
    await someOtherFunction();
}