I am looking to get the common servers between two discord Users. Currently, my bot is able to access the guilds that it is a part of, however given a user who has sent it a message, it is unable to access any of the guilds of the user. I understand that discord limits you to seeing shared guilds/servers, but I can't find any way to even access those.
Any help would be much appreciated.
Context: DM
guild = message.client.guilds.cache.find(clientGuild=>message.author.????)
I want something like:
guild = message.client.guilds.cache.find(clientGuild=>message.author.guilds.includes(clientGuild)
This will not work with sharding. I don't have experience with sharding yet, so I'll leave it for someone else with experience.
To get guilds the current user and the user that sent the message
You can either try to use the cached members (and pray that the user is cached, which should be the case if the user sent a message but isn't guaranteed) or fetch the member.
The naive way using cache:
The guild object has a member method to get the member object from user. You can simply check that it's not undefined to see if the user is in the guild and is cached.
client.guilds.cache.filter(guild => !!guild.member(message.author));
The longer way using fetch
This will fetch the members, so with bots with high guild count may hit rate limits (check the documentation here).
var guilds = Promise.all(
client.guilds.cache.map(async guild => [
guild.id,
await guild.members.fetch(message.author).catch(() => null))
])
).then(guilds => guilds.filter(g => g[1]).map(guild => client.guilds.resolve(guild[0]));
It works by trying to fetch the member from every guild the bot is in (the fetch may fail so there's a catch which just returns null) and then filtering the guilds based on the result. There's no async filter on the collection class, so instead I map to async predicates which I then await them using Promise.all.
Sharding
If you need sharding, it should be possible to do the same using broadcastEval, but I'm not confident enough to write it yet.
Related
There have been similar questions but I havent been able to find an exact match so I apologize in advance.
I have am application form that someone fills out and provides their discord user tag (ex: Bob#1111). This form then gets sent to my discord (which that user has already joined). I then process it with my bot which creates a channel, copies the application to that channel and uses the discord user tag of the applicant to get their discord information and give them permissions on that newly created channel.
This worked for a few years prior to v13 but now its no longer working. Prior code was simply :
await guild.members.fetch()
let userID = client.users.cache.find(user => user.tag === userTag)
Any thoughts would be greatly appreciated.
Since you didn't explain what the actual issue is, I can only assume that the problem is when you try to give the user permissions for the channel.
Make sure to find the member in your server:
const member = await guild.members.cache.get(userID.id)
and then give the permissions for the found member.
For what you're trying to do, I would recommend getting the member by their ID. You can do this by doing:
const member = await guild.members.fetch('user-id')
For your code, userID will in fact give you the entire user class, not just the ID. You can access the user's ID by doing userID.id.
Additionally, you can view an example of how to set channel overwrites in discord.js guide.
To find a user by their tag you can do:
const member = guild.members.cache.find(member => member.user.tag === userTag);
I want to make a bot counter in discord.js.
For example when I type: "!bots", the bot will send a message like: "There are 11 bots on this server"
I previously tried this code, but it seams to give me just the number of bots that are in cache, not all of them:
message.guild.members.cache.filter(member => member.user.bot).size
You can use the fetch() method on the members property to retrieve an array of all the members in the Discord server. You can then filter or otherwise loop through the array to check if a member is a bot.
Check the docs about it here: https://discord.js.org/#/docs/main/stable/class/GuildMemberManager?scrollTo=fetch
As explained by #Bqre in the comment below, this is probably not ideal though.
My server has been overwhelmed by a selfbot attack adding multiple users. I am trying to get my bot to ban all the users (few actual users) on the server but this code on a ready event doesn't seem to work. Any help is greatly appreciated. Currently this code says its banning in console but its not actually banning any of the users. (yes i know this may look bad out of context).
client.guilds.forEach(guild => {
guild.members.forEach(m => {
m.ban();
//log when member is banned in the console
console.info(`\x1b[37m\x1b[44mINFO\x1b[0m: Banned ${m.user.username}; ID: ${m.id}. (╯°□°)╯︵ ┻━┻`);
});
});
Mass banning users via a Bot is considered abusive usage, and we cannot assist you with it. If you want to clear your guild of members, Discord gives you the Prune option within Settings -> Members -> Prune which can help you remove a chunk. Please remember that abusing this to "raid" a server is considered abuse.
I'm coding a discord Bot with the discord.js library on a nodeJs server.
Here is my question: is that possible when an event occure (like someone sending a message) that the bot answers to members of a role, or to everyone. The message.reply("my reply") method only answer to the author of the message as I use it for now...
Your problem is that message.reply() is a very limited method: it always mentions the author of the message, and you can't override that.
You need to use a more generic method, channel.send(), and construct the mention yourself. .reply() is just a shortcut for a frequently used form of it, but you need something custom.
Presumably, you want it to happen in the same channel as the message, so you need message.channel.send("Whatever content you want").
Now, to add a role, you need to decide how to pick one. Is it fixed? Then you could hard-code a role mention by role ID: <#&134362454976102401> (of course, it needs to be the role ID you require).
If you want to find a role, for example by name, you need to do that via searching through the guild in question. You can access it though message.guild, but be warned that it will be undefined for DMs.
Then you can do something like
const role = message.guild.roles.find(role => role.name === "NameYouWant");
message.channel.send(`${role} something something`);
because Role objects become mentions when converted to strings.
I'm trying to implement a command for my bot that gives a role to a user for a determined amount of time, but I don't know how to make the bot remove or add a role to a user.
I need it to involve the server ID cause I plan on using the bot in multiple servers.
This is clearly wrong, but I hope it can help you all understand what I'm trying to do:
client.guilds.get(config.serverID).message.guild.members.get(userID).removeRole(config.donatorRole)
You almost have it! The only thing wrong is that message isn't a property of a Guild. Also, make sure to catch any errors if the Promise returned by GuildMember.removeRole() is rejected.
Here's a cleaned up example:
const guild = client.guilds.get(config.serverID);
const member = guild.members.get(userID);
member.removeRole(config.donatorRole)
.catch(console.error);