正如标题所述,我想删除特定用户的最后一封邮件(使用discord.js
)。例如:--delete 4 @user
直到现在我都这样做:
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
client.on("message", async message => {
if (command == "delete") {
if (message.member.hasPermission("MANAGE_MESSAGES")) {
if (args.length > 1) {
let mentioned = message.guild.member(message.mentions.users.first()) || message.guild.members.get(args[0]);
for (let i = 0; i < args[0]; i++) {
message.channel.fetchMessage(mentioned.id).then(msg => {
msg.delete()
.then(msg => console.log(`Deleted message from ${mentioned.id}`))
.catch(console.error);
}).catch(console.error);
}
} else if (args.length < 2) {
message.channel.fetchMessages()
.then(function(list) {
message.channel.bulkDelete(parseInt(args));
}, function(err) {
message.channel.send("ERROR: ERROR CLEARING CHANNEL.");
})
}
} else {
await message.channel.send(noHasPermission);
}
}
});
答案 0 :(得分:0)
这种事情可能是用户的(根据AnIdiotsGuide Purge命令)
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, or just an amount, of messages to purge!');
// Fetch 100 messages (will be filtered and lowered up to max amount requested)
message.channel.fetchMessages({
limit: 100,
}).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));
});
用法是:!purge @user 10 , or !purge 25