The welcome message is sent to my server? - discord

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.

Related

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

Get the number of bots in a server

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!")

Gmail REST API Thread Search not Giving Expected Results

We have built an Email Audit Application for one of our customers. The app utilizes the Gmail REST API and provides a front-end interface that allows users with permission to run audit queries based on a selected date-ranges. The date-range that is provided by the user is utilized in the Email Thread search query.
We have noticed, however, that the API is showing a discrepancy between the threads that are returned and the actual items that are in the inbox of an audited user. For example, in order to collect everything within 1 day, say 4/28, we need to expand the audit range from 4/27-4/29.
The documentation for the Gmail REST API provides no explanation nor highlighting of this behavior. Is this an issue with the API or are there additional parameters that perhaps can specify the time-zone for which we can search for these email threads?
Below you will find a snippet of code that is utilized to grab such email threads:
def GrabAllThreadIDs(user_email, after_date, before_date):
query = "in:inbox " + "after:" + after_date + " " + "before:" + before_date
# Create the Gmail Service
gmail_service = create_gmail_service(user_email)
raw_thread_response = ListThreadsMatchingQuery(gmail_service, 'me', query)
for item in raw_thread_response:
all_ids.append(item['id'])
return all_ids
======================================================
def ListThreadsMatchingQuery(service, user_id, query=''):
"""List all Threads of the user's mailbox matching the query.
Args:
service: Authorized Gmail API service instance.
user_id: User's email address. The special value "me"
can be used to indicate the authenticated user.
query: String used to filter messages returned.
Eg.- 'label:UNREAD' for unread messages only.
Returns:
List of threads that match the criteria of the query. Note that the returned
list contains Thread IDs, you must use get with the appropriate
ID to get the details for a Thread.
"""
try:
response = service.users().threads().list(userId=user_id, q=query).execute()
threads = []
if 'threads' in response:
threads.extend(response['threads'])
while 'nextPageToken' in response:
page_token = response['nextPageToken']
response = service.users().threads().list(userId=user_id, q=query,
pageToken=page_token).execute()
threads.extend(response['threads'])
return threads
except errors.HttpError, error:
print 'An error occurred: %s' % error
======================================================
That is how the Advanced search is designed. after gives messages sent after 12:00 AM (or 00:00), and before gives messages before the given date. Asking for after:2015/04/28 and before:2015/04/28 would result in a non-existent timespan.
I like to use the alternate form after:<TIME_IN_SECONDS_SINCE_THE_EPOCH>. If you would like to get all the messages received on 2015/04/28 you would write after:1430172000 before:1430258399 (2015/04/28 00:00 to 2015/04/28 23:59:59)

GAE Datastore ID

I've created two different Entities, one a User and one a Message they can create. I assign each user an ID and then want to assign this ID to each message which that user creates. How can I go about this? Do I have to do it in a query?
Thanks
Assuming that you are using Python NDB, you can having something like the following:
class User(ndb.Model):
# put your fileds here
class Message(ndb.Model):
owner = ndb.KeyProperty()
# other fields
Create and save a User:
user = User(field1=value1, ....)
user.put()
Create and save a Message:
message = Message(owner=user.key, ...)
message.put()
Query a message based on user:
messages = Message.query().filter(Message.owner==user.key).fetch() # returns a list of messages that have this owner
For more information about NDB, take a look at Python NDB API.
Also, you should take a look at Python Datastore in order to get a better understanding of data modeling in App Engine.

Resources