如何使我的 Discord.js v14 机器人在斜杠命令有效时停止显示 “应用未响应”?

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

How can i get my discord.js v14 bot to stop saying "The application did not respond" if the slash command works?

问题

以下是您要翻译的代码部分:

  1. const Discord = require('discord.js');
  2. const { EmbedBuilder, SlashCommandBuilder } = require('discord.js');
  3. module.exports = {
  4. data: new SlashCommandBuilder()
  5. // command name
  6. .setName('totalfrozencheckouts')
  7. // command description
  8. .setDescription('Add up every message in the frozen checkouts channel after a specific message ID')
  9. .addStringOption(option =>
  10. option.setName('messageid')
  11. .setDescription('The message ID')
  12. .setRequired(true)),
  13. async execute(interaction) {
  14. const channel = '<#' + process.env.FROZENCHECKOUTS + '>';
  15. const messageId = interaction.options.getString("messageid");
  16. // 检查通道提及是否有效
  17. if (!channel.startsWith('<#') || !channel.endsWith('>')) {
  18. return interaction.channel.send(`无效的频道提及。请使用以下格式:${this.usage}`);
  19. }
  20. // 从频道提及中提取频道ID
  21. const channelId = channel.slice(2, -1);
  22. // 尝试从请求的通道和消息ID获取消息
  23. interaction.guild.channels.cache.get(channelId).messages.fetch({ after: messageId })
  24. .then(messages => {
  25. // 创建一个包含数字的消息内容数组
  26. const numbers = messages.map(message => message.content).filter(content => !isNaN(content));
  27. // 检查是否有消息
  28. if (numbers.length === 0) {
  29. return interaction.channel.send(`在 ${channel} 中找不到消息ID后的消息:https://discord.com/channels/1059607354678726666/1060019655663689770/${messageId}`);
  30. }
  31. // 累加消息
  32. const sum = numbers.reduce((accumulator, currentValue) => accumulator + parseInt(currentValue), 0);
  33. // 创建一个嵌入对象
  34. const embed = new EmbedBuilder()
  35. .setColor(0x4bd8c1)
  36. .setTitle('今天 #frozen-checkouts 的总结是:')
  37. .addFields(
  38. {name: '总结', value: sum.toString() , inline: true},
  39. )
  40. .setThumbnail('https://i.imgur.com/7cmn8uY.png')
  41. .setTimestamp()
  42. .setFooter({ text: 'Frozen ACO', iconURL: 'https://i.imgur.com/7cmn8uY.png' });
  43. // 发送嵌入对象
  44. interaction.channel.send({embeds: [embed]});
  45. })
  46. .catch(error => {
  47. console.error(error);
  48. interaction.channel.send('尝试获取消息时发生错误。请稍后再试。');
  49. });
  50. }
  51. }

希望这能帮助您理解代码并解决问题。

英文:

i have multiple commands that work perfectly fine but i always get this message in return.

如何使我的 Discord.js v14 机器人在斜杠命令有效时停止显示 “应用未响应”?

here is the code for that command. it works perfectly fine i guess it just doesn't respond to the interaction even though i feel like it should be?

how can i get it to ignore this message or reply properly?

  1. const Discord = require(&#39;discord.js&#39;);
  2. const { EmbedBuilder, SlashCommandBuilder } = require(&#39;discord.js&#39;);
  3. module.exports = {
  4. data: new SlashCommandBuilder()
  5. // command name
  6. .setName(&#39;totalfrozencheckouts&#39;)
  7. // command description
  8. .setDescription(&#39;Add up every message in the frozen checkouts channel after a specific message ID&#39;)
  9. .addStringOption(option =&gt;
  10. option.setName(&#39;messageid&#39;)
  11. .setDescription(&#39;The message ID&#39;)
  12. .setRequired(true)),
  13. async execute(interaction) {
  14. const channel = &#39;&lt;#&#39; + process.env.FROZENCHECKOUTS + &#39;&gt;&#39;;
  15. const messageId = interaction.options.getString(&quot;messageid&quot;);
  16. // Check if the channel mention is valid
  17. if (!channel.startsWith(&#39;&lt;#&#39;) || !channel.endsWith(&#39;&gt;&#39;)) {
  18. return interaction.channel.send(`Invalid channel mention. Please use the format: ${this.usage}`);
  19. }
  20. // Extract the channel ID from the channel mention
  21. const channelId = channel.slice(2, -1);
  22. // Try to fetch the messages from the requested channel and message ID
  23. interaction.guild.channels.cache.get(channelId).messages.fetch({ after: messageId })
  24. .then(messages =&gt; {
  25. // Create an array of the message contents that are numbers
  26. const numbers = messages.map(message =&gt; message.content).filter(content =&gt; !isNaN(content));
  27. // Check if there are any messages
  28. if (numbers.length === 0) {
  29. return interaction.channel.send(`No messages were found in ${channel} after message ID https://discord.com/channels/1059607354678726666/1060019655663689770/${messageId}`);
  30. }
  31. // Adds up the messages
  32. const sum = numbers.reduce((accumulator) =&gt; accumulator + 1, 1);
  33. // Create an embed object
  34. const embed = new EmbedBuilder()
  35. .setColor(0x4bd8c1)
  36. .setTitle(`Total Checkouts in #frozen-checkouts for today is:`)
  37. .addFields(
  38. {name: &#39;Total Checkouts&#39;, value: sum.toString() , inline: true},
  39. )
  40. .setThumbnail(&#39;https://i.imgur.com/7cmn8uY.png&#39;)
  41. .setTimestamp()
  42. .setFooter({ text: &#39;Frozen ACO&#39;, iconURL: &#39;https://i.imgur.com/7cmn8uY.png&#39; });
  43. // Send the embed object
  44. interaction.channel.send({embeds: });
  45. })
  46. .catch(error =&gt; {
  47. console.error(error);
  48. interaction.channel.send(&#39;An error occurred while trying to fetch the messages. Please try again later.&#39;);
  49. });
  50. }
  51. }

I don't really know what to try because it literally works I just don't know how to get it to either ignore that message or respond with nothing. It doesn't break the bot its just annoying to look at.

答案1

得分: 1

使用interaction.reply而不是interaction.channel.send来回复。

英文:

Use interaction.reply instead of interaction.channel.send to reply.

huangapple
  • 本文由 发表于 2023年1月8日 05:15:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75043833.html
匿名

发表评论

匿名网友

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

确定