Delete all users from the server with one command - node.js

Delete all users from the server with one command, how? Usually, a ban is deleted and only a specific user, but how to delete all with one command?

Heĺlo, as first, you need to get the guild, where you want to kick/ban every member:
//Get the guild by ID
const guild = client.guilds.get("ID");
//Get the guild where a message was sent
const guild = message.guild;
Now if you want to KICK every user in that guild:
//Kick every user in that guild using forEach()
guild.members.forEach(member => member.kick);
Or if you want to BAN every user in that guild:
//Ban every user in that guild using forEach()
guild.members.forEach(member => member.ban);

Related

(discord.js v13) If channel exists

I'm making a ticketing system with buttons. I trying to if user opened a ticket, user can't open a new ticket. Im tried some code:
//ticket channel is created with **"help: " + interaction.user.username** name
if (interaction.guild.channels.fetch('channel name'))
//working with only id
if (interaction.guild.channels.cache.get(c=>c.name==='channel name'))
//reacted nothing
The discord.js channels cache is a Discord.Collection of values, meaning that it is a JS map with some extra quality of life methods added by Discord.js. Discord Collections are keyed using the snowflake id with the value being whatever object is being stored with that id (in this case the channel you want). This means that the method fetch on the collection of channels can always only be passed an ID as stated here in the docs. This would also mean that the Map.get() method you try to use in the second attempt will not return anything as the channel is not keyed by its name, its keyed by a snowflake id.
You can use a piece of code like this one I used in a discord moderation bot to find and return a channel by name if it exists in the cache.
/**
* Get the log channel for the provided guild.
* #param {Discord.Guild} guild The guild to get the log channel for.
*/
#getLogChannel = (guild) => {
const guildLogChannel = guild.channels.cache
.find(channel => channel.name === 'guild-logs');
if (!guildLogChannel) return false;
return guildLogChannel;
}
If the channel has not yet been cached you have no other option than to pass that channel into the interaction as an option, fetch the channel by its snowflake id, or fetch all channels for all guilds the bot is in inside your client.on('ready', () => {}) handler. The last option is what I chose to do for the bot that the above code snippet is taken from.

How to give own bot permission to see categories?

My bot has a Command that creates a Category, and inside it, a Text and Voice channel. On creating the Category I overwrite the permission to let only a certain role to see that category and its channels.
Everything until this works fine. My problem comes when I want my bot to see the text channel, so he can read and interact with messages. My bot doesn't have any extra role aside from the Discord App one.
So far, this are the solutions I tried:
Set Bot to Administrator: This solution works, but want to discart it, since I don't know if it is recommended to set your bot as Administrator, and I believed there should be other ways to solve the problem.
Add Permission to Category, setting my bot role to let him read and write messages, like this:
await category.set_permissions(bot_role, read_messages=True, send_messages=True, connect=True)
This throws an discord.errors.Forbidden: 403 Forbidden (error code: 50001): Missing Access error, which I don't understand since I have all permission set in my oauth page (see photo below).
I have also tried to give the same permissions to the text channel, but it throws the same error.
All I really want is my Bot to read messages in that text channel, since he won't respond to any command if the Bot can't see the channel.
Edit:
This is how I created the category and channels:
async def prepare_game(ctx):
guild = ctx.guild
role = await guild.create_role(name=role_name)
category = await guild.create_category('Game')
await category.set_permissions(role, read_messages=True, send_messages=True, connect=True, speak=True)
text_channel = await guild.create_text_channel('Board', category=category, sync_permissions=True)
voice_channel = await guild.create_voice_channel('Room', category=category, sync_permissions=True)
Then at the end, I added the await category.set_permissions(bot_role, read_messages=True, send_messages=True, connect=True)line, where bot_role is my app bot role
Try setting the permission overwrites for ctx.guild.me (the bot user itself).
From the docs, set_permissions will accept a Role or a Member.
For example:
async def prepare_game(ctx):
guild = ctx.guild
role = await guild.create_role(name=role_name)
category = await guild.create_category('Game')
await category.set_permissions(role, read_messages=True, send_messages=True, connect=True, speak=True)
await category.set_permissions(ctx.guild.me, read_messages=True, send_messages=True, speak=True)
text_channel = await guild.create_text_channel('Board', category=category, sync_permissions=True)
voice_channel = await guild.create_voice_channel('Room', category=category, sync_permissions=True)

Discord.js V12 Sending an embed to a channel in a different guild

HI i was making a command where it sends an embed to a channel with an id however the command works normally when used in the guild where it has that channel but when the command is used in a different guild it throws error "TypeError: Cannot read property 'send' of undefined" is there any way to fix this? i want the command to be usable in other guilds as well
const channel = message.guild.channels.cache.find(c => c.id === "746423099871985755")
const exampleEmbed = new Discord.MessageEmbed()
.setColor('#FFFF33')
.setTitle(message.member.user.tag)
.setAuthor(`Order ID: ${ticketID}`)
.setDescription(order)
.setThumbnail(message.author.avatarURL())
.setTimestamp()
.setFooter(`From ${message.guild.name} (${message.guild.id})`);
channel.send(exampleEmbed);
Message#guild is the current guild. So if you want to send the message into a channel from another guild you need to search for that guild in your cache from your client. You can do this by writing:
const guild = message.client.guilds.cache.get("/* Your guild id */");
const channel = guild.channels.cache.get("/* Your channel id */");
You can also do it a bit compacter:
const channel = message.client.guilds.cache.get("/* Your guild id */").channels.cache.get("/* Your channel id */");
If you want to search with channel-names:
const channel = message.client.guilds.cache.get("/* Your guild id */").channels.cache.find(c => c.name === "/* Channel name */");
Since every single channel on Discord has a different ID, you can't get two different channel objects with one ID. What you can do instead is use a channel's name (the channel will have to have the same name in both servers), or you can hard code both channels manually.
e.g.
// using names
const channel = message.guild.channels.cache.find(c => c.name === "channel-name");
// hardcoding IDs
const channel = message.guild.channels.cache.find(c => c.id === "ID1" || c.id === "ID2");
This should also work message.guild.channels.cache.get('GUILD_ID').send(embed);

Check if the user adding a reaction has a role

I want to check if the user reacting to a message has a role or not, so that if the user dont have a rank the bot will ignore the rest of the command, but I dont know how.
example: if a user with the admin rank reacted with :eggsa: emoji, the bot would continue with the command, but if i only had member rank the bot would ignore me.
client.on('messageReactionAdd', reaction => {
const eggsa = client.emojis.find(emoji => emoji.name === "eggsa");
if (reaction.emoji.name === 'eggsa') {
const message = reaction.message;
const kanal = reaction.message.guild.channels.find('name', 'sitater');
var embed = new Discord.RichEmbed()
.setAuthor(reaction.message.content)
.setTimestamp()
.setFooter(reaction.message.author.username, reaction.message.author.avatarURL)
kanal.send({embed});
}
});
the code works in this stage, only everyone can use it
I would be really grateful if someone could help me >:)
If you look at the messageReactionAdd docs you can see that along with the reaction, you can also get the user which added the reaction. So instead of
client.on('messageReactionAdd', reaction => {
You'd have
client.on('messageReactionAdd', (reaction, user) => {
So then you have the user and which is of type User. Because the user isn't of type GuildMember you first need to fetch the correct Guild member before you can check his/her role.
The easiest way to do this is by getting the Message to which the reaction was added with const msg = reaction.message;. Then you can get the guild from the message with const guild = msg.guild;. Now you can access the guild members with const guildMembers = guild.members;. Lastly you'd need to find the correct member with const guildMember = guildMembers.get(user.id);.
Now that you have your Guild Member you can access his/her Roles and thus check whether he/she has or does not have a certain role

Discord.js Backdoor command

I've seen my bot join many more servers, but it seems like some are abusing it.
I want the bot to make a one time use invite to a server that I am not on, but my bot is. Once I am on the server I can just remove it. It would be like so:
^backdoor "guild id". I am very new to coding. Thanks!
There are 2 possible ways of doing this, but both are reliant on the permissions that the bot has in that guild.
guildid has to be replaced with an ID or an variable equivilant to the ID
Way 1:
let guild = client.guilds.get(guildid):
if (!guild) return message.reply("The bot isn't in the guild with this ID.");
guild.fetchInvites()
.then(invites => message.channel.send('Found Invites:\n' + invites.map(invite => invite.code).join('\n')))
.catch(console.error);
Way 2:
let guild = client.guilds.get(guildid):
if (!guild) return message.reply("The bot isn't in the guild with this ID.");
let invitechannels = guild.channels.filter(c=> c.permissionsFor(guild.me).has('CREATE_INSTANT_INVITE'))
if(!invitechannels) return message.channel.send('No Channels found with permissions to create Invite in!')
invitechannels.random().createInvite()
.then(invite=> message.channel.send('Found Invite:\n' + invite.code))
There would also be the way of filtering the channels for SEND_MESSAGE and you could send a message to the server.
Instead of entering the guild and then removing it, it would be simpler to just make the bot leave the guild, using Guild.leave()
// ASSUMPTIONS:
// guild_id is the argument from the command
// message is the message that triggered the command
// place this inside your command check
let guild = client.guilds.get(guild_id);
if (!guild) return message.reply("The bot isn't in the guild with this ID.");
guild.owner.send("The bot has been removed from your guild by the owner.").then(() => {
guild.leave();
});

Resources