Get the number of bots in a server - discord

I'm making a public member count for my server, which will be displayed in the name of a locked voice channel on top of the channel list.
Though, to make the count even more accurate, I would like not to include the bots into the member count. I assume the best way to do that would be to subtract the number of bots from the total number of members in the server.
The thing is, I don't know how to get the number of bots in a server (only the total number of members).
Thank you all in advance :D

guild.members returns the list of members of the guild,
member.bot which has the attribute bot is False for user accounts.

Notice you will need to turn on member intents for your bot for this to work:
#client.command()
async def bot_count(ctx):
members = ctx.author.guild.members
bot_count = 0
for i in members:
member = i.bot
if member == True:
bot_count += 1
await ctx.send(f"Server has {bot_count} bots!")

Related

Maximum number of users returned per page when calling transitive member api on a group

I have the following query to get the transitive members in a group:
await _graphServiceClient
.Groups[groupId]
.TransitiveMembers
.Request()
.Top(999)
.GetAsync();
For larger groups, response takes a longer time as there are a number of pages being returned. Currently I have set the top value as 999. What is the maximum number of users that can be returned per page?
The maximum allowed value can vary depending on the kind of collection you're going to ask. Most of them have a maximum value of 999 by using top(999), but a few have lower values (If you pick a too high number you get back an error message containing the maximum allowed value).
The retrieved page object has a property NextPageRequest which will be not null, if further data is available and you can get it by calling it:
var moreMembers = await members.NextPageRequest.GetAsync();
You can do this in a while loop, till the property NextPageRequest is null to get a list of all members.
Similar issue - https://github.com/microsoftgraph/microsoft-graph-docs/issues/13233
Hope this help
Thanks

The welcome message is sent to my server?

I have specified the channel ID in my code. When a new member joins my server, the message is sent to the channel where I want it, but when a member of my server joins another server where my bot is located, the welcome message is sent to my server, although the member is already on it and has just joined another server. My question is, how can I fix the bot not sending the welcome message to my server when a member joins another server?
And my second question is, how can I set up that, for example, administrators from other servers can choose where to send the welcome message to their servers?
#client.event
async def on_member_join(member):
embed = discord.Embed(color=0x6c5ce7)
embed.set_thumbnail(url=member.avatar_url)
embed.add_field(name=f"Welcome, {member.name}#{member.discriminator}!\n", value=f":white_small_square: Check the <#848929002281631744> channel and read the rules!\n"
f":white_small_square: Then look in the <#848929105109057567> channel and follow the instructions!\n\n"
f"Have fun on our Server!", inline=True)
embed.set_footer(text=f"New Member: {member.name}#{member.discriminator}", icon_url=member.avatar_url)
await client.get_channel(865655108230447175).send(embed=embed)
One way you could do it would be to check whether the guild the member is joining is your guild. The best and most accurate way to do this would be via id.
#client.event
async def on_member_join(member):
# check if the guild the member is joining is equal to your guild's id
if member.guild.id != YOUR GUILD ID:
return
# if it's the same guild id, it continues
embed = discord.Embed(color=0x6c5ce7)
# etc, other code
If you want to use it in multiple of your own servers, you could consider using a json, which may store the guild ids as well as the channel ids, and use a similar concept as seen in the code above.

discord.py How to get a server the bot is in with the most members

I wanted to know if there was a way to get a server with the most members that the bot is in. So like for example if the bot was in servers, a, b, c, and d, And server A had the most members, how can I make it show that it's the biggest server.
I'm assuming that this will be a command. Here is a sample code to answer your prompt:
#bot.command() # your client/bot variable. IDK which you have so I put bot.
async def biggest_server(ctx):
guilds = {len(guild.members):guild for guild in bot.guilds}
max_members, max_guild = max(guilds.items())
await ctx.send(f"{max_guild.name} is the biggest server I am in! They have {max_members} members!")
Quick Documentation Links:
bot.guilds - Get the guilds that the bot is in. I use dictionary comprehension so that the guild object and it's member length stay paired.
max - Get the maximum number of members (index 0). This returns the tuple of the biggest server. I unpack it to two variables.

Check permissions of default_role for a text_channel with discord.py

Using the discord.py library, is it possible to check if the #everyone role has send_message permissions in a given channel within the server? My goal is to avoid 're-opening' a channel.
That is simple, just check the Permissions for the everyone role.
everyone_role = guild.roles[0] #ctx.guild in commands and message.guild in on_message
if everyone_role.permissions.send_messages:
#everyone can send messages generally
# checking if overwrites exist for the everyone role
everyone = [e for e in channel.changed_roles if role.position == 0]
if everyone:
if everyone.permissions.send_messages:
#everyone can send message in this channel
else:
#they can't
Note: everyone is the lowest role in the hierarchy order and has position 0 and index 0 in role lists.
References:
changed_roles
Permissions
guild.roles

Finding how many members are in a role

How would I find how many members are in a specific role.
let memberCount = message.guild.roles.cache.find(r => r.name === "Lobby 1").members.size;
This only returns 0 or 1 depending on if the user has the role.
You can fetch the user list of the server and then loop through the array checking if one of the users has the role. If a user has the role, you can increment a global var.
Hope I could help
ShadowLp174

Resources