当我运行代码,我有一个错误:C:Program Filesnodejsnode.exe .index.js
调试器。等待调试器断开连接…
d:GitHubPROJECTScodageprojectsBOTSDiscordJsAllForOne BotJsindex.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
的代码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);
将其中一个段落改为this
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);
}
}
});