为什么应用程序在 discord.js 中没有响应?

huangapple go评论130阅读模式
英文:

why app is not responding with discord.js?

问题

我想编写一个代码,可以发送一个带有所有命令的嵌入式消息,所以我编写了一个可以工作的代码,但在发送嵌入式消息后,它在嵌入式消息上方显示了一个错误:embed。以下是代码:

  1. const { Client, GatewayIntentBits, CommandInteraction, EmbedBuilder } = require('discord.js')
  2. const client = new Client({
  3. intents: [
  4. GatewayIntentBits.Guilds,
  5. // ...
  6. ]
  7. });
  8. client.on('ready', async () => {
  9. console.log(`Logged in as ${client.user.tag}!`);
  10. const commands = [
  11. {
  12. name: 'pomoc',
  13. description: 'Wyświetla wszystkie komendy'
  14. }
  15. ];
  16. const commandData = commands.map(command => ({
  17. name: command.name,
  18. description: command.description
  19. }));
  20. const commandManager = await client.application?.commands.set(commandData);
  21. console.log(`Registered ${commandManager.size} slash commands`);
  22. });
  23. client.on('interactionCreate', interaction => {
  24. if (!interaction.isCommand()) return;
  25. if (interaction.commandName === 'pomoc') {
  26. const embed = new EmbedBuilder()
  27. .setTitle('some title')
  28. .setDescription('some description')
  29. interaction.channel.send({ embeds: [embed] });
  30. }
  31. });
  32. client.login(process.env.CLIENT_TOKEN);

我尝试了这种方法:

  1. interaction.channel.send({
  2. embeds: [{
  3. title: 'some title',
  4. description: 'some description',
  5. image: { url: 'image url' }
  6. }]
  7. })
英文:

I wanted to write a code that will send an embed with all the commands so I wrote a code that works but after sending the embed it shows an error above the embed: embed. Here's the code:

  1. const { Client, GatewayIntentBits, CommandInteraction, EmbedBuilder } = require('discord.js')
  2. const client = new Client({
  3. intents: [
  4. GatewayIntentBits.Guilds,
  5. // ...
  6. ]
  7. });
  8. client.on('ready', async () => {
  9. console.log(`Logged in as ${client.user.tag}!`);
  10. const commands = [
  11. {
  12. name: 'pomoc',
  13. description: 'Wyświetla wszystkie komendy'
  14. }
  15. ];
  16. const commandData = commands.map(command => ({
  17. name: command.name,
  18. description: command.description
  19. }));
  20. const commandManager = await client.application?.commands.set(commandData);
  21. console.log(`Registered ${commandManager.size} slash commands`);
  22. });
  23. client.on('interactionCreate', interaction => {
  24. if (!interaction.isCommand()) return;
  25. if (interaction.commandName === 'pomoc') {
  26. const embed = new EmbedBuilder()
  27. .setTitle('some title')
  28. .setDescription('some description')
  29. interaction.channel.send({embeds: })
  30. }
  31. });
  32. client.login(process.env.CLIENT_TOKEN);

I tried this method:

  1. interaction.channel.send({
  2. embeds: [{
  3. title: 'some title',
  4. description: 'some description',
  5. image: {url: 'image url'}
  6. }]
  7. })

答案1

得分: 0

问题在于您在交互渠道中发送消息,而不是回复交互本身。

在调用斜杠命令时,Discord会向您的服务器发送请求并等待响应。这个错误是由于响应超时,因为您的服务器会发出单独的请求,而不是响应初始请求。

而不是使用 interaction.channel.send(details),您应该使用 interaction.reply(details) 来回复交互。

英文:

The issue is that you're sending a message in the interaction channel, rather than replying to the interaction itself.

Upon calling a slash command, discord will send a request to your server and await a response. This error is due to the response timing out, as your server makes a separate request rather than responding to the initial request.

Rather than using interaction.channel.send(details), you should be using interaction.reply(details) to respond to the interaction.

huangapple
  • 本文由 发表于 2023年2月26日 20:49:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572081.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定