activity
,
ping
,user
)都没有出现:
中缺少命令 我的项目目录的结构如下:
commands/
└── utility/
├── ping.js
├── check_activity.js
├── user.js
server/
├── api.js
├── models/
└── activity.js
deploy-commands.js
index.js
命令在服务器中工作正常,但在DMS中不起作用,我不知道为什么。
index.js
(不包括MongoDB相关零件)
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, Events, GatewayIntentBits, MessageFlags } = require('discord.js');
const { token } = require('./config.json');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildPresences,
],
});
client.commands = new Collection();
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
console.warn(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
const errorMessage = 'There was an error while executing this command!';
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: errorMessage, flags: MessageFlags.Ephemeral });
} else {
await interaction.reply({ content: errorMessage, flags: MessageFlags.Ephemeral });
}
}
});
client.login(token);
在线搜索类似问题,但我找不到任何有用的东西。检查我是否需要对DMS的不同权限或意图。
这是因为我的机器人依赖于“ guild”相关的函数
,它在DMS中不起作用?如果是这样,我应该更改如何支持服务器和DMS?我是JavaScript和Discord应用程序开发的新手,因此,任何帮助都将不胜感激!谢谢!
我也是我的deploy-commands.js
:
const { REST, Routes } = require('discord.js');
const { clientId, guildId, token } = require('./config.json');
const fs = require('node:fs');
const path = require('node:path');
const commands = [];
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
commands.push(command.data.toJSON());
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
const rest = new REST().setToken(token);
(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);
const data = await rest.put(
Routes.applicationCommands(clientId),
{ body: commands },
);
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error(error);
}
})();
contexts
integration_types
问题。 (文献链接)
命令
contexts:
0
= guild,
1
= in dms with bot直接,2
=组DMS/私人通道(如果用户在其帐户上授权机器人)。