discord.js消息批量删除仅删除限制而不是指定的限制

时间:2020-06-29 12:26:42

标签: javascript bots discord.js

所以我试图让机器人删除指定的数量而不是限制,该限制是我要删除的最大数量,它仅删除了限制数量。我尝试了几件事,但无济于事。

async run(message, args) {
    var obliviateRoles = [
        'Dev',
        'Bot Dev',
        'Moderator'
    ]
    var hasRole = false;
    obliviateRoles.forEach(findrole =>{
        if(message.member.roles.cache.some(role => role.name === findrole)) hasRole = true; //if user has role, sets bool to true
    })
    var mention = message.mentions.users.first()

    if(hasRole === true){
const user = message.mentions.users.first();
// Parse Amount
const amount = !!parseInt(message.content.split(' ')[1]) ? parseInt(message.content.split(' ')[1]) : parseInt(message.content.split(' ')[2])

if (!amount) return message.reply('Must specify an amount to delete!');

if (!amount && !user) return message.reply('Must specify a user and amount!');
// Fetch 100 messages (will be filtered and lowered up to max amount requested)
message.channel.messages.fetch({
    limit: 5,
   }).then((messages) => {
 if (user) {
 const filterBy = user ? user.id : Client.user.id;
 
messages = messages.filter(m => m.author.id === filterBy).array().slice(0, amount);
 }
 message.channel.bulkDelete(messages).catch(error => console.log(error.stack));
});
}else{
    message.reply('This Spell is too powerful for you.')

  }
 }
}

任何想法都会受到赞赏。

2 个答案:

答案 0 :(得分:2)

通过将channel.fetch()传递到bulkDelete,您将删除通道中的每条消息,因为channel.bulkDelete(n)返回了整个文本通道消息。 bulkDelete确实接受数值。因此,如果n等于非负整数,则可以调用var num = 15 // whatever number you want message.channel.bulkDelete(num).catch(error => console.log(error.stack));

例如

const proxy = new Proxy(unsafeWindow.HTMLCanvasElement.prototype.getContext, {
    apply(target, thisArg, args) {
        const ctx = Reflect.apply(...arguments);

        const prototype = Object.getPrototypeOf(ctx);
        const descriptors = Object.getOwnPropertyDescriptors(prototype);

        Object.defineProperties(prototype, {
            arc: {
                value(x, y, radius, startAngle, endAngle) {
                        const { a, b, c, d, e, f } = ctx.getTransform();
                        const scale = 2;
                        ctx.setTransform(a * scale, b, c, d * scale, e, f);

                    return descriptors.arc.value.call(this, ...arguments);
                }
            },
        });

        return ctx;
    }
});
unsafeWindow.HTMLCanvasElement.prototype.getContext = proxy;

答案 1 :(得分:0)

检查是否对您有所帮助(我对您的代码做了一些改进)

async run(message, args) {
if(['Dev','Bot Dev','Moderator'].some((r) => message.member.roles.cache.has(role => role.name == r))){
const user = message.mentions.users.first();
// Parse Amount
const amount = !!parseInt(message.content.split(' ')[1]) ? parseInt(message.content.split(' ')[1]) : parseInt(message.content.split(' ')[2])

if (!amount) return message.reply('Must specify an amount to delete!');

if (!amount && !user) return message.reply('Must specify a user and amount!');
// Fetch 100 messages (will be filtered and lowered up to max amount requested)
message.channel.messages.fetch({
    limit: amount > 5 ? 5 : amount,
   }).then((messages) => {
messages = messages.filter(m => m.author.id === user.id).array();
message.channel.bulkDelete(messages).catch(error => console.log(error.stack));
});
}else{
    message.reply('This Spell is too powerful for you.')
  }
 }
}