首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Discord.js异步和等待

Discord.js异步和等待
EN

Stack Overflow用户
提问于 2021-10-16 15:01:14
回答 1查看 52关注 0票数 0

当我运行代码时,我附加了一个错误:C:\Program Files\nodejs\node.exe .\index.js调试器。正在等待调试器断开连接...

代码语言:javascript
运行
复制
d:\GitHub\PROJECTS\codage\projects\BOTS\DiscordJs\AllForOne BotJs\index.js:34
            await interaction.reply(`Server name:${interaction.guild.name}\nTotal members: ${interaction.guild.memberCount}`);
            ^^^^^

SyntaxError: await is only valid in async functions and the top level bodies of modules
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
    at node:internal/main/run_main_module:17:47
Process exited with code 1

代码

代码语言:javascript
运行
复制
    const Client = new Discord.Client({ 
    intents: [
        Discord.Intents.FLAGS.GUILDS,
        Discord.Intents.FLAGS.GUILD_MESSAGES,
        Discord.Intents.FLAGS.DIRECT_MESSAGES
    ]
});
const token = require("./config.json");
const prefix = "!";
const invite = "https://discord.com/api/oauth2/authorize?client_id=898625628004319232&permissions=8&scope=bot%20applications.commands";

const commands = [
    new SlashCommandBuilder().setName("ping").setDescription("resend pong").addUserOption(option => option.setName("utilisateur").setDescription("utilisateur que vous souhaiter mentionner").setRequired(false)),
    new SlashCommandBuilder().setName("server").setDescription("Replies with server info!"),
    new SlashCommandBuilder().setName("user").setDescription("Replies with user info!"),
]

Client.on("ready", () => {

    //Client.application.commands.create(data);
    Client.guilds.cache.get("864543215650996234").commands.create(data);

    console.log("Ready!");
});

Client.on("interactionCreate", interaction => {
    if(interaction.isCommand()){
        if(interaction.commandName === "ping"){
            interaction.reply("pong");
        }
        else if (interaction.commandName === 'server'){
            await interaction.reply(`Server name:${interaction.guild.name}\nTotal members: ${interaction.guild.memberCount}`);
        }
        else if (interaction.commandName === 'user'){
            await interaction.reply("Your tag:" + interaction.user.tag + "\nYour id:" + interaction.user.id);
        }
    }
});

Client.on("guildMemberAdd", message => {
    message.channel.send("Bienvenue !")
})

Client.on("messageCreate", message => {
    if(message.author.bot) return;

    if(message.content === prefix  + "ping"){
        message.reply("pong");
    }
});

Client.on("messageCreate", message => {
    if (message.author.bot) return;

    if(message.content === prefix + "help"){
        message.channel.send("*le bot est en développement*\n**Les commandes**\n(prefix) + invite | Donne l'invitation du bot")
    }
});

Client.on("messageCreate", message => {
    if(message.author.bot) return;

    if(message.content === prefix  + "invite"){
        message.channel.send("my invite is " + invite);
    }
});

Client.on("messageCreate", message => {
    if (message.author.bot) return;

    if(message.content === prefix + "help"){
        const embed = new Discord.MessageEmbed()
            .setAuthor("CraftX#9999", "https://discord.com/assets/3c6ccb83716d1e4fb91d3082f6b21d77.png", "https://github.com/")
            .setColor("#001cfc")
            .setTitle("**Commands list**")
            .setURL("https://discord.js.org/")
            .setDescription("The list of the bot commands")
            .setFooter("Bot in devloppement")
            .setThumbnail("https://discord.com/assets/3c6ccb83716d1e4fb91d3082f6b21d77.png")
            .addField("!help", "__ping__  | send 'pong'\n__invite__ | Donne l'invitation du bot")
            .setImage("https://discord.com/assets/3c6ccb83716d1e4fb91d3082f6b21d77.png")
            .setTimestamp();

            
        message.channel.send({ embeds: [embed]});
    }
});



Client.login(token);
EN

回答 1

Stack Overflow用户

发布于 2021-10-16 16:04:29

把其中一段改成这样

代码语言:javascript
运行
复制
Client.on("interactionCreate", async (interaction) => {
    if(interaction.isCommand()){
        if(interaction.commandName === "ping"){
            interaction.reply("pong");
        }
        else if (interaction.commandName === 'server'){
            await interaction.reply(`Server name:${interaction.guild.name}\nTotal members: ${interaction.guild.memberCount}`);
        }
        else if (interaction.commandName === 'user'){
            await interaction.reply("Your tag:" + interaction.user.tag + "\nYour id:" + interaction.user.id);
        }
    }
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69596782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档