【发布时间】:2021-10-27 00:32:05
【问题描述】:
我在“我的 discord.js 机器人即使没有任何错误也不会回复用户消息”之前发布了一个问题。好吧,该解决方案有效,但仅适用于消息,而不适用于命令。
我还在机器人的 OAuth2 中激活 applications.commands 范围。
我正在使用节点 16.7.0,这些是已安装的软件包:
- discord.js@dev 13.2.0
- @discordjs/builders 0.5.0
- @discordjs/rest 0.1.0-canary.0
- discord-api-types 0.22.0
- dotenv 10.0.0
- fs 0.0.1-安全
控制台没有显示任何错误消息,但它显示了这些,这意味着它正在工作:
Started refreshing application (/) commands.
Successfully reloaded application (/) commands.
Ready! Logged in as {mydiscord_bot_tag}
这些是我的机器人的文件:
discord-bot/
├── node_modules
├── .env
├── public/
|── index.js
└── functions/
|──handleCommands.js
└──handleEvents.js
└── events/
|──interactionCreate.js
└──ready.js
└── commands/
|── Information/
└── ping.js
└── Moderation
├── package-lock.json
└── package.json
index.js 的代码
const { Client, Intents, Collection } = require('discord.js');
const fs = require('fs');
require('dotenv').config();
const client = new Client({ intents:[Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
client.commands = new Collection();
const functions = fs.readdirSync('./src/functions').filter(file => file.endsWith('.js'));
const commandsFolder = fs.readdirSync('./src/commands').filter(file => file.endsWith('.js'));
const eventFiles = fs.readdirSync('./src/events').filter(file => file.endsWith('.js'));
client.once('ready', () => {
client.user.setPresence({ activities: [{ name: 'i am stuck ' }], status: 'dnd' });
});
(async () => {
for (const file of functions) {
require(`./functions/${file}`)(client);
}
client.handleEvents(eventFiles, './src/commands');
client.handleCommands(commandsFolder, './src/events');
client.login(process.env.token);
})();
functions/handleCommands.js 的代码
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const fs = require('fs');
const clientId = '878988418665816156';
const guildId = '879887194268008520';
module.exports = (client) => {
client.handleCommands = async (commandsFolders, path) => {
client.commandArray = [];
for (const folder of commandsFolders) {
const commandFiles = fs.readdirSync(`${path}/${folder}`).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`../commands/${folder}/${file}`);
client.commands.set(command.data.name, command);
client.commandArray.push(command.data.toJSON);
}
}
const rest = new REST({ version: '9' }).setToken(process.env.token);
(async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: client.commandArray },
);
console.log('Successfully reloaded application (/) commands.');
}
catch (error) {
console.error(error);
}
})();
};
};
functions/handleEvents.js 的代码
module.exports = (client) => {
client.handleEvents = async (eventFiles) => {
for (const file of eventFiles) {
const event = require(`../events/${file}`);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args, client));
}
else {
client.on(event.name, (...args) => event.execute(...args, client));
}
}
};
};
events/interactionCreate.js 的代码
module.exports = {
name: 'interactionCreate',
async execute(interaction, client) {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction);
}
catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
},
};
事件/ready.js
module.exports = {
name: 'ready',
once: true,
execute(client) {
console.log(`Ready! Logged in as ${client.user.tag}`);
},
};
commands/Information/ping.js 的代码
const { SlashCommandBuilder } = require('@discordjs/builders');
module.exports = {
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with Pong!'),
async execute(interaction) {
await interaction.reply('Pong!');
},
};
很抱歉所有这些巨大的代码它们是我的整个文件夹:")
希望得到答复,谢谢!
【问题讨论】:
-
最好只包含相关代码。拥有所有文件会使问题变得不必要地冗长。
-
我认为问题将在这些文件中,这就是为什么我放了所有这些代码
标签: javascript node.js discord.js bots