【问题标题】:Discord.js: Export Ban list command not workingDiscord.js:导出禁令列表命令不起作用
【发布时间】:2023-03-14 14:39:01
【问题描述】:

我最近开始研究具有 3 个主要功能的不和谐禁止机器人:

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

即使逻辑看似正确,斜线命令也不起作用。

我正在遵循 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'
}

【问题讨论】:

  • 尝试控制台记录 fe 变量。它可能是未定义的或空字符串

标签: discord discord.js command export


【解决方案1】:

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

确实,new_paste() 内部函数中的data 没有返回到fe

(我基本上搞砸了返回范围)

这是修复和范围解析后的最终代码

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

【讨论】:

    【解决方案2】:

    我认为你的 npm 包 better-pastebin 有错误。我不熟悉那个npm包,所以我无法确定它是否对你有错误,但我认为如果你改变npm包,错误不会出现。

    【讨论】:

      猜你喜欢
      • 2021-07-24
      • 2021-04-18
      • 2021-07-27
      • 2021-11-19
      • 2018-07-28
      • 1970-01-01
      • 2023-03-03
      • 2020-08-12
      • 2020-11-14
      相关资源
      最近更新 更多