首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >由于通道错误,批量删除discord.js不起作用

由于通道错误,批量删除discord.js不起作用
EN

Stack Overflow用户
提问于 2021-10-03 20:54:36
回答 1查看 71关注 0票数 0
代码语言:javascript
复制
import DiscordJS, { TextChannel, Intents, Message, Channel } from 'discord.js'
import dotenv from 'dotenv'
dotenv.config()

//sets prefix to be used when running bot commands
const prefix = '~';

//This lets the discord bot know what your intentions are using this bot. Hence the guilds, guilds messages and message reactions
const client = new DiscordJS.Client({
    intents: [
        Intents.FLAGS.GUILDS,
        Intents.FLAGS.GUILD_MESSAGES,
        Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
        Intents.FLAGS.DIRECT_MESSAGES
    ]
})


//Deleting messages in bulk
client.on("message", (message) => {
    if (message.content.toLowerCase().startsWith(prefix + "clearchat")) {
        async function clear() {
            message.delete();
            var fetched = await message.channel.messages.fetch({limit: 99})
            message.channel.bulkDelete(fetched);
        }
        clear();
    }
});

我正在尝试批量删除,但问题是message.channel.bulkDelete(fetched);.channel部分说它是一个TextBasedChannel,而不是TextChannel。我早些时候问过别人这件事,他们说我应该用TextChannel的时候,却在用DMChannel。我知道它们是不同的类,但我不确定如何在代码中使用DMChannel而不是TextChannel。我不确定如何解决这个问题,如果有人有链接告诉我有什么不同,我会很感激的。只是很难理解DMChannel,因为我是在服务器中使用机器人,而不是在直接消息中。我只是有点迷惑

编辑:我可以按预期清除聊天,但现在我得到了一个DiscordAPIError。我能捕捉到错误吗?以下是错误消息:

编辑2:这是上述错误消息之后的内容

代码语言:javascript
复制
 DiscordAPIError: You can only bulk delete messages that are under 14 days old.
    at RequestHandler.execute (C:\Users\theod\Desktop\DiscordBot\node_modules\discord.js\src\rest\RequestHandler.js:298:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async RequestHandler.push (C:\Users\theod\Desktop\DiscordBot\node_modules\discord.js\src\rest\RequestHandler.js:50:14)
    at async TextChannel.bulkDelete (C:\Users\theod\Desktop\DiscordBot\node_modules\discord.js\src\structures\interfaces\TextBasedChannel.js:312:7) {
  method: 'post',
  path: '/channels/872986149294047234/messages/bulk-delete',
  code: 50034,
  httpStatus: 400,
  requestData: { json: { messages: [Array] }, files: [] }
EN

回答 1

Stack Overflow用户

发布于 2021-10-03 23:28:15

消息可能在DM中,您不能批量删除DM通道中的消息。首先检查消息是否在行会中:

代码语言:javascript
复制
import type { NewsChannel, TextChannel, ThreadChannel } from 'discord.js';

type GuildTextBasedChannel = TextChannel | NewsChannel | ThreadChannel

if (message.content.toLowerCase().startsWith(prefix + "clearchat")) {
    if (message.guild) {
        async function clear() {
            message.delete();
            var fetched = await message.channel.messages.fetch({limit: 99})
            (message.channel as GuildTextBasedChannel).bulkDelete(fetched, true);
        }
        clear();
    }
}

不幸的是,Discord.js的类型不是很好,所以需要类型断言as GuildTextBasedChannel来让TypeScript知道如果message.channel不是null,那么message.guild必须是一个基于行会文本的通道。

如果需要,您可以定义一个帮助器函数来消除对此类型断言的需要:

代码语言:javascript
复制
import type { Message } from 'discord.js';

function inGuild(message: Message): message is Message & { readonly channel: GuildTextBasedChannel } {
    return message.guild !== null;
}

if (inGuild(message)) {
  message.channel.bulkDelete(99, true); // no type assertion needed
}

或者,您可以使用类似以下内容:

代码语言:javascript
复制
import type * as Discord from 'discord.js';

type Message = Discord.Message &
  (
    | {
        readonly channel: GuildTextBasedChannel;
        readonly guild: Discord.Guild;
      }
    | {
        readonly channel:
          | Discord.DMChannel
          // if you're using partials
          | Discord.PartialDMChannel;
        readonly guild: null;
      }
  );

client.on("messageCreate", (_message) => {
    const message = _message as Message;
    // rest of code...
    // type assertion not required
});

bulkDelete(fetched, true)中的true参数意味着Discord.js将自动过滤超过14天的邮件。不一致不允许您批量删除超过14天的邮件,这是您的错误消息告诉您的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69428808

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档