Discord.py - How to track if someone joined from a specific invite?
Yes, it is, but you'll have to do the heavy lifting.
Here's the reference for the Invite
object. It has a uses
attribute which stores the amount of times it's been used to join your server: so if you store all the possible invites for your server in memory, and check which one went up whenever a user joins (in on_member_join()
, you'll be able to tell which one they used.
This medium article shows a way to do it. There's no other way I'm aware of, or that I can find in the documentation.
Getting the users total invites in Discord.py
You've almost got the right idea!
on_message event usage:
@bot.event
async def on_message(message):
if message.content.startswith('!invites'):
totalInvites = 0
for i in await message.guild.invites():
if i.inviter == message.author:
totalInvites += i.uses
await message.channel.send(f"You've invited {totalInvites}
member{'' if totalInvites == 1 else 's'} to the server!")
Command decorator usage:
@bot.command()
async def invites(ctx):
totalInvites = 0
for i in await ctx.guild.invites():
if i.inviter == ctx.author:
totalInvites += i.uses
await ctx.send(f"You've invited {totalInvites} member{'' if totalInvites == 1 else 's'} to the server!")
First I'm iterating through each invite in the guild, checking who created each one. If the creator of the invite matches the user that executed the command, it then adds the number of times that invite has been used, to a running total.
You don't need to include the {'' if totalInvites == 1 else 's'}
, that's just for the odd case that they've invited 1 person (turns member
into the plural - members
).
References:
Guild.invites
- the code originally didn't work because I forgot this was a coroutine (had to be called()
andawait
ed).Invite.uses
Invite.inviter
commands.command()
- F-strings Python 3.6+
Get person who invited the discord bot discord.py
There isn't any way yet to know who invited the bot.
What you can do is DM the Server Owner when its added to a Server.
Related Topics
Pyspark Replace All Values in Dataframe With Another Values
Cv2.Videocapture.Open() Always Returns False
How to Append Data Using Openpyxl Python to Excel File from a Specified Row
How to Center a Window on the Screen in Tkinter
Spliting a Row to Multiple Row Pyspark
How to Read a List of Parquet Files from S3 as a Pandas Dataframe Using Pyarrow
How to Track the Number of Times a Function Is Called
Generate List of Quarters Betweeen Given Dates
How to Clear All Widgets from a Tkinter Window in One Go Without Referencing Them All Directly
Robot Framework Using Python, Key Press Without Selecting Any Button or Element in the Page
How to Connect to a Remote Windows Machine to Execute Commands Using Python
How to Remove a Single Quotes from a List
Grab a Number After a String in a File
Spliting a Row to Multiple Row Pyspark
Overlay a Smaller Image on a Larger Image Python Opencv
Run Multiple Python File Concurrently
Read CSV from Google Cloud Storage to Pandas Dataframe
Splitting Timestamp Column into Separate Date and Time Columns