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

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

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

问题

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

const Discord = require('discord.js');
const { EmbedBuilder, SlashCommandBuilder } = require('discord.js');

module.exports = {
  data: new SlashCommandBuilder()
    // command name
    .setName('totalfrozencheckouts')
    // command description
    .setDescription('Add up every message in the frozen checkouts channel after a specific message ID')
    .addStringOption(option =>
      option.setName('messageid')
        .setDescription('The message ID')
        .setRequired(true)),
    async execute(interaction) {
        const channel = '<#' + process.env.FROZENCHECKOUTS + '>';
        const messageId = interaction.options.getString("messageid");
        
        // 检查通道提及是否有效
        if (!channel.startsWith('<#') || !channel.endsWith('>')) {
            return interaction.channel.send(`无效的频道提及。请使用以下格式:${this.usage}`);
        }
        
        // 从频道提及中提取频道ID
        const channelId = channel.slice(2, -1);
        
        // 尝试从请求的通道和消息ID获取消息
        interaction.guild.channels.cache.get(channelId).messages.fetch({ after: messageId })
            .then(messages => {
            // 创建一个包含数字的消息内容数组
            const numbers = messages.map(message => message.content).filter(content => !isNaN(content));
        
            // 检查是否有消息
            if (numbers.length === 0) {
                return interaction.channel.send(`在 ${channel} 中找不到消息ID后的消息:https://discord.com/channels/1059607354678726666/1060019655663689770/${messageId}`);
            }
        
            // 累加消息
            const sum = numbers.reduce((accumulator, currentValue) => accumulator + parseInt(currentValue), 0);

            // 创建一个嵌入对象
            const embed = new EmbedBuilder()
            .setColor(0x4bd8c1)
            .setTitle('今天 #frozen-checkouts 的总结是:')
            .addFields(
                {name: '总结', value: sum.toString() , inline: true},
            )
            .setThumbnail('https://i.imgur.com/7cmn8uY.png')
            .setTimestamp()
            .setFooter({ text: 'Frozen ACO', iconURL: 'https://i.imgur.com/7cmn8uY.png' });

            // 发送嵌入对象
            interaction.channel.send({embeds: [embed]});
            })

            .catch(error => {
            console.error(error);
            interaction.channel.send('尝试获取消息时发生错误。请稍后再试。');
            });
        }
    }

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

英文:

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?

const Discord = require(&#39;discord.js&#39;);
const { EmbedBuilder, SlashCommandBuilder } = require(&#39;discord.js&#39;);
module.exports = {
data: new SlashCommandBuilder()
// command name
.setName(&#39;totalfrozencheckouts&#39;)
// command description
.setDescription(&#39;Add up every message in the frozen checkouts channel after a specific message ID&#39;)
.addStringOption(option =&gt;
option.setName(&#39;messageid&#39;)
.setDescription(&#39;The message ID&#39;)
.setRequired(true)),
async execute(interaction) {
const channel = &#39;&lt;#&#39; + process.env.FROZENCHECKOUTS + &#39;&gt;&#39;;
const messageId = interaction.options.getString(&quot;messageid&quot;);
// Check if the channel mention is valid
if (!channel.startsWith(&#39;&lt;#&#39;) || !channel.endsWith(&#39;&gt;&#39;)) {
return interaction.channel.send(`Invalid channel mention. Please use the format: ${this.usage}`);
}
// Extract the channel ID from the channel mention
const channelId = channel.slice(2, -1);
// Try to fetch the messages from the requested channel and message ID
interaction.guild.channels.cache.get(channelId).messages.fetch({ after: messageId })
.then(messages =&gt; {
// Create an array of the message contents that are numbers
const numbers = messages.map(message =&gt; message.content).filter(content =&gt; !isNaN(content));
// Check if there are any messages
if (numbers.length === 0) {
return interaction.channel.send(`No messages were found in ${channel} after message ID https://discord.com/channels/1059607354678726666/1060019655663689770/${messageId}`);
}
// Adds up the messages
const sum = numbers.reduce((accumulator) =&gt; accumulator + 1, 1);
// Create an embed object
const embed = new EmbedBuilder()
.setColor(0x4bd8c1)
.setTitle(`Total Checkouts in #frozen-checkouts for today is:`)
.addFields(
{name: &#39;Total Checkouts&#39;, value: sum.toString() , inline: true},
)
.setThumbnail(&#39;https://i.imgur.com/7cmn8uY.png&#39;)
.setTimestamp()
.setFooter({ text: &#39;Frozen ACO&#39;, iconURL: &#39;https://i.imgur.com/7cmn8uY.png&#39; });
// Send the embed object
interaction.channel.send({embeds: });
})
.catch(error =&gt; {
console.error(error);
interaction.channel.send(&#39;An error occurred while trying to fetch the messages. Please try again later.&#39;);
});
}
}

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:

确定