【问题标题】:await is only valid in async function Discord bot reaciton roleawait 仅在异步函数 Discord bot 反应角色中有效
【发布时间】:2021-08-10 14:48:57
【问题描述】:

我是初学者,我想在不和谐机器人上编写反应角色,但它不起作用,请帮助我

SyntaxError: await 仅在异步函数中有效

/////////////////////////////////////// ///////////////////////////////////////// ///////////////////////////////////////// ///////////////////////////////////////// ///////////////////////////////////////// //////////////////////////////////////////

    module.exports = {
name: "reactionrole",
description: "Sets up a reaction role message",
execute(message, args, Discord, client) {
    const channel = "845246330678280212";
    const czechRole = message.guild.roles.cache.find(role => role.name === "Czech");
    const slovakiaRole = message.guild.roles.cache.find(role => role.name === "Slovakia");

    const czechFlag = ":flag_cz:";
    const slovakiaFlag = ":flag_sk:";

    let embed = new Discord.MessageEmbed()
        .setcolor("#e42643")
        .setTitle("Choose your State!")
        .setDescription("Choose your state!\n\n"
            + "${czechFlag} for Czech state\n"
            + "${slovakiaFlag} for Slovakia state");

    let MessageEmbed = await message.channel.send(embed);
    messageEmbed.react(czechFlag);
    messageEmbed.react(slovakiaFlag);

    client.on("messageReactionAdd", async (reaction, user) => {
        if (reaction.message.partial) await reaction.message.fetch();
        if (reaction.partial) await reaction.fetch();
        if (user.bot) return;
        if (!reaction.message.guild) return;

        if (reaction.message.channel.id == channel) {
            if (reaction.flag.name === czechFlag) {
                await reaction.message.guild.members.cache.get(user.id).roles.add(czechRole);
            }
            if (reaction.flag.name === slovakiaFlag) {
                await reaction.message.guild.members.cache.get(user.id).roles.add(slovakiaRole);
            }
        } else {
            return;
        }
    });

    client.on("messageReactionRemove", async (reaction, user) => {
        if (reaction.message.partial) await reaction.message.fetch();
        if (reaction.partial) await reaction.fetch();
        if (user.bot) return;
        if (!reaction.message.guild) return;

        if (reaction.message.channel.id == channel) {
            if (reaction.flag.name === czechFlag) {
                await reaction.message.guild.members.cache.get(user.id).roles.remove(czechRole);
            }
            if (reaction.flag.name === slovakiaFlag) {
                await reaction.message.guild.members.cache.get(user.id).roles.remove(slovakiaRole);
            }
        } else {
            return;
        }
    });
}

}

【问题讨论】:

  • 你能指出错误出现在代码的什么地方吗?
  • execute 不是异步方法,但您在其中使用了await

标签: javascript asynchronous async-await discord bots


【解决方案1】:

这行可能会导致问题:

let MessageEmbed = await message.channel.send(embed);

您只能将await 语句放在async 函数中。上面的语句属于execute(...) 函数,它不是异步的。

【讨论】:

    猜你喜欢
    • 2020-12-23
    • 2020-07-31
    • 2020-06-29
    • 1970-01-01
    • 2020-08-16
    • 2020-09-07
    • 2019-09-12
    • 1970-01-01
    • 2021-05-01
    相关资源
    最近更新 更多