您如何批量删除除固定消息之外的消息?

时间:2023-01-02
本文介绍了您如何批量删除除固定消息之外的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个名为 clear 的脚本.此脚本按预期工作.

I made a script called clear. This script works as intended.

但是我的清晰脚本如何跳过频道中的固定消息?

But how can my clear script skip the pinned messages in the channel?

这是我的脚本:

const discord = require("discord.js");

module.exports.run = async (bot, message, args) => {


    if (message.deletetable) {
        message.delete(); 
    }

    // Member doesn't have permission
    if (!message.member.hasPermission("MANAGE_MESSAGES")) {
        return message.channel.send("You can't delete messages...") // .then(m => m.delete(5000));
    }

    // Check if args[0] is a number
    if (isNaN(args[0]) || parseInt(args[0]) <= 0) {
        return message.channel.send("Yeah... That's not a number? I also can't delete 0 messages by the way.") // .then(m => m.delete(5000));

    }

    // Maybe the bot can't delete messages
    if (!message.guild.me.hasPermission("MANAGE_MESSAGES")) {
        return message.channel.send("Sorry... I can't delete messages.") // .then(m => m.delete(5000));
    }

    let deleteAmount;

    if (parseInt(args[0]) > 100) {
        deleteAmount = 100;
    } else {
        deleteAmount = parseInt(args[0]);
    }

    message.channel.bulkDelete(deleteAmount, true)
        .then(deleted => message.channel.send(`I deleted `${deleted.size}` messages.`))
        .catch(err => message.channel.send(`Something went wrong... ${err}`));
}   


module.exports.help = {
  name: "clear"
}

我希望有人可以帮助我,因为这将是我清晰脚本的一个很好的补充.

I hope someone can help me, because this would be a nice addition to my clear script.

推荐答案

获取频道的所有消息,然后pinned消息,用filter做一个diff,只保留非pinned消息,然后删除这些那些.

Get all messages of the channel, then pinned messages, do a diff with filter, keep only non pinned messages, then delete these ones.

client.on("message", async message => {
  if (message.author.bot) return;

  if (message.content === "/bulk") {
    const allMessages = await message.channel.messages.fetch()
    const deletable = allMessages.filter(message => !message.pinned)
    await message.channel.bulkDelete(deletable, true)
  }
});

这篇关于您如何批量删除除固定消息之外的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:.drawImage 函数为画布抛出“TypeError: Image or Canvas 下一篇:检查消息是否从 DM 发送不工作

相关文章

最新文章