提问人:Titiplex 提问时间:11/14/2023 更新时间:11/14/2023 访问量:15
javascript 中的 Discord 机器人无法识别从另一个 js 文件加载的函数
Discord bot in javascript doesn't recognize function loaded from another js file
问:
我目前正在为一个副项目开发一个不和谐机器人,我既不是一个经验丰富的程序员,也不习惯 js 或 discord .js。
我有这个代码:
require('dotenv').config();
const fs = require('fs');
const path = require('path');
const {Client, Collection, GatewayIntentBits, EmbedBuilder, SlashCommandBuilder, PermissionsBitField, Permissions} = require('discord.js');
const client = new Client({intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildPresences, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.GuildMessageReactions, GatewayIntentBits.GuildMessageTyping, GatewayIntentBits.DirectMessages, GatewayIntentBits.DirectMessageReactions, GatewayIntentBits.DirectMessageTyping]});
client.commands = new Collection();
const commandsPath = path.join(__dirname, 'economy');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
async function loadCommands(client) {
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ('data' in command && 'execute' in command) {
await client.application?.commands.create(command.data);
client.commands.set(command.data.name, command);
console.log(`[INFO] Loaded command ${command.data.name} from ${filePath}`);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
loadCommands(client);
client.on("ready", (x) => {
console.log(`${x.user.tag} is ready!`);
client.user.setActivity("I work my ass off");
const help = new SlashCommandBuilder()
.setName('help')
.setDescription('Get help for the Tiluraiv bot.');
client.application.commands.create(help);
})
client.on('interactionCreate', (interaction) => {
if(!interaction.isChatInputCommand()) return;
if(interaction.commandName=='help') {
interaction.reply('help');
}
})
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = interaction.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 });
}
});
client.login(process.env.TOKEN);
这将得到以下文件:
const { SlashCommandBuilder } = require('@discordjs/builders');
module.exports = {
data: new SlashCommandBuilder()
.setName('balance')
.setDescription('desc'),
async execute(interaction) {
await interaction.reply('balance');
},
};
在终端中,我有“[INFO] 从 C:\MY COMPUTER\src\economy\balance 加载的命令余额.js
但是每当我不和谐并尝试 / 命令时,即使 /help 命令有效并出现在列表类型中,我也没有得到任何平衡命令。
有没有人看到问题可能出在哪里?
我已经看了一个小时了,我只想崩溃一切。我尝试了多种方法,我也尝试遵循他们在这里所说的 https://discordjs.guide/creating-your-bot/command-deployment.html#where-to-deploy.我还尝试了一个朋友的代码,由于太旧而不起作用。
答: 暂无答案
评论