Discord.Py Show Who Invited a User

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 () and awaited).
  • 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



Leave a reply



Submit