我尝试用 javascript 制作一个不和谐的机器人。我的问题是不和谐中的命令不起作用。有人可以帮我解决这个问题吗?
这是我的错误:
/applications/1235114104566579231/guilds/1183059923391754411/commands {
body: [
{
options: [],
name: 'ping',
name_localizations: undefined,
description: 'Return my ping!',
description_localizations: undefined,
default_permission: undefined,
default_member_permissions: undefined,
dm_permission: undefined,
nsfw: undefined
}
]
}
这是我的代码:
ping.js(命令)
const { SlashCommandBuilder } = require("discord.js");
module.exports = {
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('Return my ping!'),
async execute(interaction, client) {
const message = await interaction.deferReply({
fetchReply: true
});
const newMessage = `API Latency: ${client.ws.ping}\nClient Ping: ${message.createdTimestamp - interaction.createdTimestamp}`
await interaction.editReply({
content: newMessage
});
}
}
InteractionCreate.js
module.exports = {
name: 'interactionCreate',
async execute(interaction, client) {
if (interaction.isChatInputCommand()) {
const { commands } = client;
const { commandName } = interaction;
const command = commands.get(commandName);
if (!command) return;
try {
await command.execute(interaction, client);
} catch (error) {
console.error(error);
await interaction.reply({
content: `Something went wrong while executing this command...`,
ephemeral: true,
});
}
}
},
};
ready.js
module.exports = {
name: 'ready',
once: true,
async execute(client) {
console.log(`Ready ${client.user.tag} is logged in and online`);
}
}
handlecommands.js
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const fs = require('fs');
module.exports = (client) => {
client.handleCommands = async () => {
const commandFolders = fs.readdirSync('./commands');
for (const folder of commandFolders) {
const commandFiles = fs.readdirSync(
`./commands/${folder}`)
.filter(file => file.endsWith('.js'));
const { commands, commandArray } = client;
for (const file of commandFiles) {
const command = require(`../../commands/${folder}/${file}`);
commands.set(command.data.name, command);
commandArray.push(command.data.toJSON());
}
}
const clientId = '1235114104566579231';
const guildId = '1183059923391754411';
const rest = new REST({ version: '9' }).setToken(process.env.token);
try {
console.log(Routes.applicationGuildCommands(clientId, guildId), {
body: client.commandArray,
});
console.log("Successfully reloaded application (/) commands.");
} catch (error) {
console.error(error);
}
};
};
handleEvents.js
const fs = require('fs');
module.exports = (client) => {
client.handleEvents = async () => {
const eventFolders = fs.readdirSync(`./events`);
for (const folder of eventFolders) {
const eventFiles = fs.readdirSync(
`./events/${folder}`)
.filter(file => file.endsWith('.js'));
switch (folder) {
case "client":
for (const file of eventFiles) {
const event = require(`../../events/${folder}/${file}`);
if (event.once) client.once(event.name, (...args) => event.execute(...args, client));
else client.on(event.name, (...args) => event.execute(...args, client));
}
break;
default:
break;
}
}
};
};
bot.js(索引)
require('dotenv').config();
const { token } = process.env;
const { Client, Collection, GatewayIntentBits } = require("discord.js");
const fs = require("fs");
const client = new Client({ intents: GatewayIntentBits.Guilds });
client.commands = new Collection();
client.commandArray = [];
const functionFolders = fs.readdirSync(`./functions`);
for (const folder of functionFolders) {
const functionFiles = fs
.readdirSync(`./functions/${folder}`)
.filter((file) => file.endsWith('.js'))
for (const file of functionFiles) require(`./functions/${folder}/${file}`)(client);
}
client.handleEvents();
client.handleCommands();
client.login(token);
package.json
"name": "erstesprojekt",
"version": "1.0.0",
"description": "",
"main": "bot.js",
"scripts": {
"test": "node ."
},
"keywords": [],
"author": "Luis Schroeder",
"license": "ISC",
"dependencies": {
"@discordjs/rest": "^2.2.0",
"chalk": "^4.1.2",
"discord-api-types": "^0.37.83",
"discord.js": "^14.14.1",
"dotenv": "^16.4.5"
}
}
有人可以帮我解决这个错误吗?
您的机器人似乎缺少权限。您可以尝试查看您在开发门户和 index.js 文件中允许的意图吗?您需要添加成员和消息权限。根据我的想法,这应该可以解决错误。另外,尝试使用意图位字段代替网关意图位会效果更好。