Leave a Guild if there are more bots then members - discord.js

So I have been trying to make a way to have my Discord bot leave a guild if there are more bots then Members, I have looked everywhere and can't find anything about it and people are not very helpful, Can I get some help on this?

In discord.js v12 you do it like this:
const memberCount = guild.members.cache.filter((member) => !member.user.bot).size;
const botCount = guild.members.cache.filter((member) => member.user.bot).size;
if (botCount > memberCount) {
guild.leave().catch((err) => {
console.log(`there was an error leaving the guild: \n ${err.message}`);
});
}
First you'll get both the count of the actual guild members and the count of the bots. Don't forget the cache property in v12!
Then we can check which number is larger and possibly leave the guild.
You might want to run this code on <Client>.on("message", (message) => {});

Related

discord.js - How would i get all guilds member count and leaving the server if it is below a certain member count

So I'm trying to make my bot so I can make it leave all servers under a certain member count and I would prefer if I could Do It using eval and I'm doing this so when I want to verify my bot It gets denied due to inorganic growth the member count I want it to be set is 50 if anyone could help that would be appreciated 😉
This code should do the trick
client.guilds.cache.forEach(guild => {
const memberCount = guild.members.cache.filter(member => !member.user.bot).size
// This filters out bots and only counts true members
if (memberCount < 50) {
guild.leave()
console.log(`Left the ${guild.name} guild because they only had ${memberCount} members!`)
}
})

Check if a user with a specific role reacted the latest bot message | Discord.js

i need to make my bot check if someone with admin role or a specific role reacted the bot's latest message
i made a suggestion command for my bot and i want to the bot check if anyone with the #Admin role reacted the latest bot message of #suggestions channel, then when a user that has the #Admin role react the suggestion, make the bot send me a DM saying something like: Accepted your suggestion!
Here is something that may help:
client.on('messageReactionAdd', async (reaction, user) {
if(reaction.message.channel.id !== 'suggestion channel id') return;
let channel = reaction.message.channel;
let msg = await channel.messages.fetch({limit: 1});
if(!msg || msg.id !== reaction.message.id) return;
if(reaction.message.guild.member(user).roles.cache.some(r => r.id === 'admin role id')) {
user.send('Your suggestion was accepted.')
//You may have said this wrong, but if you want the person who suggested it to be DMd
//You will need to somehow save their name (or id which can never change), let’s say you put it in the footer of an embed for the suggestion
let userID = reaction.message.embeds[0].footer;
msg.guild.members.cache.find(m => m.user.id === userID).send('Accepted your suggestion!')
}
})
I’d like to know if this doesn’t work because I didn’t get to test it. It may have some errors, but hopefully not

Give Role when a member joins the voice channel. discord.js

Im trying to make discord.js code. When a member joins the voice channnel I want the bot to give them a role, when the member leaves, i want the role to be removed. thanks for your help.
const newChannelID = newState.channelID;
const oldChannelID = oldState.channelID;
if (oldChannelID === "835280102764838942"){
member.send('role given');
let role = message.guild.roles.cache.find(r => r.id === "835279162293747774");
member.roles.add(835279162293747774);
} else if(newChannelID === "835280102764838942"){
member.send('role removed');
member.roles.remove(835279162293747774);
}
})
There's a couple of things you need to clean up in your code. I'm going to assume you're using the voiceStateUpdate event to trigger your command.
Most importantly, (logic-wise) you actually need to flip-flop the way you add and remove roles. Currently, the general code states that if the user left the voice channel (thus oldChannelID = actual vc ID), then it would actually give the user a role. This seems to be the opposite of your intention, thus you'd simply swap out the code in each if/else if statement.
Second, the way you're assigning roles and adding them is incorrect. I'd recommend this resource for more info.
Third, you cannot access message if you were indeed using voiceStateUpdate as your event, since it only emits an oldState and a newState.
Lastly, you need to designate a text channel to send the message to. In my code, I manually grabbed the ID of the channel I wanted and plugged it into the code. You'll have to do the same by replacing the number strings I have with your own specific ones.
With that being said, here's the correct modified code:
client.on('voiceStateUpdate', (oldState, newState) => {
const txtChannel = client.channels.cache.get('803359668054786118'); //manually input your own channel
const newChannelID = newState.channelID;
const oldChannelID = oldState.channelID;
if (oldChannelID === "800743802074824747") { //manually put the voice channel ID
txtChannel.send('role removed');
let role = newState.guild.roles.cache.get("827306356842954762"); //added this
newState.member.roles.remove(role).catch(console.error);
} else if (newChannelID === "800743802074824747") {
txtChannel.send('role given');
let role = oldState.guild.roles.cache.get("827306356842954762"); //change this somewhat
oldState.member.roles.add(role).catch(console.error); //adding a catch method is always good practice
}
})

How do i make discord.js list all the members in a role?

if(message.content == `${config.prefix}mods`) {
const ListEmbed = new Discord.MessageEmbed()
.setTitle('Mods:')
.setDescription(message.guild.roles.cache.get('813803673703809034').members.map(m=>m.user.tag).join('\n'));
message.channel.send(ListEmbed);
}
Hey so iam making a command which displays all the members with that role but it only seems to be sending 1 of the mods
await message.guild.roles.fetch();
let role = message.guild.roles.cache.find(role => role.id == args[0]);
if (!role) return message.channel.send('Role does not exist'); //if role cannot be found by entered ID
let roleMembers = role.members.map(m => m.user.tag).join('\n');
const ListEmbed = new Discord.MessageEmbed()
.setTitle(`Users with \`${role.name}\``)
.setDescription(roleMembers);
message.channel.send(ListEmbed);
};
Make sure your command is async, otherwise it will not run the await message.guild.roles.fetch(); - this fetches all roles in the server to make sure the command works reliably.
In Discord.jsV12 the get method was redacted and find was implemented.
Aswell as this, I would highly recommend defining variables to use in embeds and for searching since it is much more easy to error trap.
If you have many members with the role, you will encounter the maximum character limit for an embeds description. Simply split the arguments between multiple embeds.

How do i get a guild by its invite link? discord.js

I've been looking all over the internet and I can't find a answer to my question,
I am trying to get a guild by it's invite link, so like I have a discord invite link such as https://discord.com/invite/ainvitecodegoeshere, Now I want my discord.js bot to get it's guild ID with using only the invite code, Is that possible? If so how can I do it?
I know its a pretty old post, but you can get the guild ID trough the discord api. You can find a link to the documentation here
If you have the invite link, you can use the api by fetching https://discord.com/api/invites/[code] where [code] is the invite code.
It will return some guild information including the guild ID, even if your bot is not in it. To get the guild object, you can use:
const guild = client.guilds.cache.get('guildID')
const Invite = "InviteCode"; // Example: bM2Ae2
client.guilds.cache.forEach(guild => { // Looping through all the guilds your bot is in.
guild.fetchInvites().then(invites => { // Fetching the invites of the guild.
invites.forEach(invite => { // Looping through all the invites of the guild.
if (invite.code == Invite) { // Checking if the invite code matches.
message.channel.send(`Found guild : ${guild.name} | ${guild.id}`);
}
})
})
});
I'd personally use a plain loop instead since you cannot stop the .forEach() loop, and it'll continue to loop through guilds even after it finds the guild you're looking for.
Note: Your bot must be in the guild you want to find.

Resources