'Unban users if their nickname contains "x.."
I'm not really sure how to make my bot unban everyone if their username contains xx, deleted.. etc.
For example, if a user's account got banned/deleted off discord, the bot will just unban them to clear the ban log, because it contains "Deleted User".
I'm providing my existing unban command which works based on IDs.
/**
* JSDOC
* @param {Discord.Client} client
* @param {Discord.Message} message
* @param {String} args
*/
module.exports.run = async (client, message, args) => {
if (!message.member.hasPermission('BAN_MEMBERS')) return message.channel.send('You are not allowed to unban members!');
if (!message.guild.me.hasPermission('BAN_MEMBERS')) return message.channel.send('I am not allowed to unban members!');
if (!args[0]) return message.reply('Please provide a user id to unban!', { allowedMentions: { repliedUser: false } });
const toUnBan = client.users.cache.get(args[0].match(/[1234567890]{18}/igm)[0]) || await client.users.fetch(args[0], true, true);
if (!toUnBan) return message.reply('You didnt provide a valid user!', { allowedMentions: { repliedUser: false } });
try {
(await message.guild.fetchBan(toUnBan));
}
catch (err) {
return message.reply('The user isnt banned!', { allowedMentions: { repliedUser: false } });
}
message.guild.members.unban(toUnBan);
Solution 1:[1]
- For unbanning users with a certain username, you need to fetch all the bans first, then filter them with the username, and then unban them.
(await message.guild.bans.fetch())
.filter(ban => ban.username.toLowerCase().includes("deleted"))
.forEach(async(ban) => {
await message.guild.bans.remove(ban.id).catch(e => console.log(e));
console.log(`Unbanned ${ban.tag}`);
});
- Reference: GuildBanManager#remove, GuildBan, GuildBanManager#fetch (all of these are from discord.js documentation)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Darshan B |
