Discord.js:导出禁令列表命令不起作用

Discord.js: Export Ban list command not working

我最近开始研究具有 3 个主要功能的 discord ban 机器人:

  1. 导出当前 Server/Guild.
  2. 中所有被禁用户的 ID
  3. 将被禁用户的 ID 导入当前公会
  4. 将禁止列表从当前服务器传输到目标服务器。 (开发中)

None 的斜杠命令有效,即使逻辑看似正确。

我正在遵循 discordjs 指南并设法制作了一个时间标签生成器机器人,这是我的第二个机器人项目。我承认我对 Javascript 不熟悉,但该指南仍然很有帮助

出口禁令清单代码如下:

const { SlashCommandBuilder } = require('@discordjs/builders');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { token, pasteUser, pastePass, pasteKey } = require('../config.json');

const paste = require('better-pastebin');

const rest = new REST({ version: '9' }).setToken(token);
const date = new Date();
paste.setDevKey(pasteKey);
paste.login(pasteUser, pastePass);

function new_paste(serverName, results) {
    const outputFile = `${serverName}-${date}.txt`;
    paste.create({
        contents: results,
        name: outputFile,
        expires: '1D',
        anonymous: 'true',
    },
    function(success, data) {
        if (success) {
            return data;
        }
        else {
            return 'There was some unexpected error.';
        }
    });
}

module.exports = {
    data: new SlashCommandBuilder()
        .setName('export-ban-list')
        .setDescription('Exports ban list of current server'),

    async execute(interaction) {
        const bans = await rest.get(
            Routes.guildBans(interaction.guildId),
        );
        await interaction.deferReply(`Found ${bans.length} bans. Exporting...`);
        console.log(`Found ${bans.length} bans. Exporting...`);

        let results = [];
        bans.forEach((v) => {
            results.push(v.user.id);
        });
        results = JSON.stringify(results);

        const fe = new_paste(interaction.serverName, results);
        return interaction.editReply(fe);

    },
};

这个命令基本上计算被禁止的用户数,制作一个数组并将其导出到 pastebin。 问题是,机器人代码一直到计算部分,但是在制作列表时,控制台向我抛出错误:

Found 13 bans. Exporting...
DiscordAPIError: Cannot send an empty message
    at RequestHandler.execute (D:\Github\Discord-Ban-Utils-Bot\node_modules\discord.js\src\rest\RequestHandler.js:298:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async RequestHandler.push (D:\Github\Discord-Ban-Utils-Bot\node_modules\discord.js\src\rest\RequestHandler.js:50:14)
    at async InteractionWebhook.editMessage (D:\Github\Discord-Ban-Utils-Bot\node_modules\discord.js\src\structures\Webhook.js:311:15)
    at async CommandInteraction.editReply (D:\Github\Discord-Ban-Utils-Bot\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:137:21)
    at async Client.<anonymous> (D:\Github\Discord-Ban-Utils-Bot\index.js:41:3) {
  method: 'patch',
  path: '/webhooks/897454611370213436/aW50ZXJhY3Rpb246ODk4ODkyNzI0NTcxMzczNjA5OmtPeGtqelQ5eUFhMnNqVzc1Q3BpMWtQZUZRdVhveGQxaHFheFJCdVFoUWNxNUk5TVpGbThEQjdWcDdyaHZyaUJPeUpsRWFlbUp0WnVLYjB5V0RtYmJCSmlNU2wwUVlka1hYMHg0bHRJbzlHelVwRmJ6VUpRaXF2YktaVDN1ZlVp/messages/@original',
  code: 50006,
  httpStatus: 400,
  requestData: {
    json: {
      content: undefined,
      tts: false,
      nonce: undefined,
      embeds: undefined,
      components: undefined,
      username: undefined,
      avatar_url: undefined,
      allowed_mentions: undefined,
      flags: undefined,
      message_reference: undefined,
      attachments: undefined,
      sticker_ids: undefined
    },
    files: []
  }
}
D:\Github\Discord-Ban-Utils-Bot\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:89
    if (this.deferred || this.replied) throw new Error('INTERACTION_ALREADY_REPLIED');
                                             ^

Error [INTERACTION_ALREADY_REPLIED]: The reply to this interaction has already been sent or deferred.
    at CommandInteraction.reply (D:\Github\Discord-Ban-Utils-Bot\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:89:46)
    at Client.<anonymous> (D:\Github\Discord-Ban-Utils-Bot\index.js:45:22)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  [Symbol(code)]: 'INTERACTION_ALREADY_REPLIED'
}

我认为你的 npm 包 better-pastebin 有错误。我对那个npm包不熟悉,所以不能确定它是否对你有错误,但我想如果你换了npm包,就不会出现这个错误了。

感谢 Jim 我使用 console.log() 来检查发生了什么。

实际上 new_paste() 中的函数 data 并没有被 return 编辑为 fe

(我基本上搞砸了 return 作用域)

这是修复和范围解决方案后的最终代码

const { SlashCommandBuilder } = require('@discordjs/builders');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { token, pasteUser, pastePass, pasteKey } = require('../config.json');

const paste = require('better-pastebin');

const rest = new REST({ version: '9' }).setToken(token);
const date = new Date();

paste.setDevKey(pasteKey);
paste.login(pasteUser, pastePass);

module.exports = {
    data: new SlashCommandBuilder()
        .setName('export-ban-list')
        .setDescription('Exports ban list of current server'),

    async execute(interaction) {
        const bans = await rest.get(
            Routes.guildBans(interaction.guildId),
        );
        await interaction.deferReply(`Found ${bans.length} bans. Exporting...`);
        console.log(`Found ${bans.length} bans. Exporting...`);

        let results = [];
        bans.forEach((v) => {
            results.push(v.user.id);
        });
        results = JSON.stringify(results);
        console.log(results);

        const outputFile = `${interaction.guild.name}-${date}.txt`;
        paste.create({
            contents: results,
            name: outputFile,
            expires: '1D',
            anonymous: 'true',
        },
        function(success, data) {
            if (success) {
                return interaction.editReply(data);
            }
            else {
                return interaction.editReply('There was some unexpected error.');
            }
        });
    },
};

最后我得到了正确的 pastebin url 作为输出。 代码托管 here