Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

javascript - How do you bulk delete messages excluding the pinned messages?

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

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

This is my script:

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)
  }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...