For the best experience of the tutorial, read everything to actually understand how this works.
Alright, hello, I am back here with another tutorial.
So username and ID accessibility is something I see as a common problem in Discord.js for newer developers. So in this thread, I am just going to go through how you can use it to make a unban command using these functions.
Alright, so first of all we need our file and the command handler, in my case this will be commands/unban.js
and the code inside would be
const Discord = require(`discord.js`);
module.exports.run = async(client, message, args) => {
}
Alright so now we got to set up the variables, we are just going to do the username/ID and reason for the unban.
const Discord = require(`discord.js`);
module.exports.run = async(client, message, args) => {
let User = args[0];
let Reason = args.slice(1).join(` `);
if (!User) return message.reply(`Who are we unbanning?`);
if (!Reason) Reason = `Unbanned by ${message.author.tag}`
}
Alright so now that we got the variables to do stuff with, we need to fetch the bans and check if the username or UserID in the first element, args[0]
, is in there, and if it is not we are just going to return a reply that the user is not banned.
const Discord = require(`discord.js`);
module.exports.run = async(client, message, args) => {
let User = args[0];
let Reason = args.slice(1).join(` `);
if (!User) return message.reply(`Who are we unbanning?`);
if (!Reason) Reason = `Unbanned by ${message.author.tag}`
message.guild.fetchBans()
.then(bans => {
if (bans.some(u => User.includes(u.username))) {
}
else if (bans.some(u => User.includes(u.id))) {
}
else {
return message.reply(`This person is not banned`);
}
});
}
Now that we got to find the banned user in a new variable. This is not needed in ID, since unban uses the ID to unban.
const Discord = require(`discord.js`);
module.exports.run = async(client, message, args) => {
let User = args[0];
let Reason = args.slice(1).join(` `);
if (!User) return message.reply(`Who are we unbanning?`);
if (!Reason) Reason = `Unbanned by ${message.author.tag}`
message.guild.fetchBans()
.then(bans => {
if (bans.some(u => User.includes(u.username))) {
let user = bans.find(user => user.username === User);
}
else if (bans.some(u => User.includes(u.id))) {
}
else {
return message.reply(`This person is not banned`);
}
});
}
Alright so now we need to actually unban them with the reason provided in the rest of the message.
const Discord = require(`discord.js`);
module.exports.run = async(client, message, args) => {
let User = args[0];
let Reason = args.slice(1).join(` `);
if (!User) return message.reply(`Who are we unbanning?`);
if (!Reason) Reason = `Unbanned by ${message.author.tag}`
message.guild.fetchBans()
.then(bans => {
if (bans.some(u => User.includes(u.username))) {
let user = bans.find(user => user.username === User);
message.guild.unban(user.id, Reason);
}
else if (bans.some(u => User.includes(u.id))) {
message.guild.unban(User, Reason);
}
else {
return message.reply(`This person is not banned`);
}
});
}
And there we got it! That’s how you can use Username and ID accessibility in a unban command!
Reply with some ideas on helpful tutorials for Discord.js and I will try my best to get it done!
Hope it helped, see you later!