discordApp(使用discord.js,node.js构建)slash命令在服务器内部工作,但在dm

问题描述 投票:0回答:1
activity

ping

user)都没有出现:

dms

中缺少命令
我的项目目录的结构如下:
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/私人通道(如果用户在其帐户上授权机器人)。
javascript mongodb discord discord.js
1个回答
0
投票
integration_types:

,该指定应该使用哪种安装命令:0表示命令将在公会中可用,而

1
表示用户授权的installss.in 您可以将上下文和集成类型结合在一起以指定多个领域 - 例如,我的机器人的“无处可用”命令之一:
{
    name: `trivia`,
    description: `Play a trivia game!`,
    integration_types: [0, 1],
    contexts: [0, 1, 2]
}

运气好!


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.