Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discord.py show who invited a user

I am currently trying to figure out a way to know who invited a user. From the official docs, I would think that the member class would have an attribute showing who invited them, but it doesn't. I have a very faint idea of a possible method to get the user who invited and that would be to get all invites in the server then get the number of uses, when someone joins the server, it checks to see the invite that has gone up a use. But I don't know if this is the most efficient method or at least the used method.

like image 237
CyberLX Avatar asked Nov 14 '25 22:11

CyberLX


2 Answers

Make a config.json file with the content of

{
    "token": "Bot token here",
    "server-id": "server id here",
    "logs-channel-id": "invite channel here"
}

then save that.

Make a channel called invites then fill in the config.json file. And then create a file called bot.py and put the contents of:

import asyncio
import datetime
import json
import os
import commands

client = discord.Client()
cfg = open("config.json", "r")
tmpconfig = cfg.read()
cfg.close()
config = json.loads(tmpconfig)

token = config["token"]
guild_id = config["server-id"]
logs_channel = config["logs-channel-id"]


invites = {}
last = ""

async def fetch():
 global last
 global invites
 await client.wait_until_ready()
 gld = client.get_guild(int(guild_id))
 logs = client.get_channel(int(logs_channel))
 while True:
  invs = await gld.invites()
  tmp = []
  for i in invs:
   for s in invites:
    if s[0] == i.code:
     if int(i.uses) > s[1]:
      usr = gld.get_member(int(last))
      testh = f"{usr.name} **joined**; Invited by **{i.inviter.name}** (**{str(i.uses)}** invites)"
      await logs.send(testh)
   tmp.append(tuple((i.code, i.uses)))
  invites = tmp
  await asyncio.sleep(4)


@client.event
async def on_ready():
 print("ready!")
 await client.change_presence(activity = discord.Activity(name = "joins", type = 2))


@client.event
async def on_member_join(meme):
 global last
 last = str(meme.id)




client.loop.create_task(fetch())
client.run(token)

Then open the terminal and run python3 bot.py. And for more help join this.

like image 123
DrNano Avatar answered Nov 17 '25 18:11

DrNano


In Discord, you're never going to 100% sure who invited the user.

Using Invite, you know who created the invite.

Using on_member_join, you know who joined.

So, yes, you could have to check invites and see which invite got revoked. However, you will never know for sure who invited since anyone can paste the same invite link anywhere.

like image 28
NinjaKitty Avatar answered Nov 17 '25 18:11

NinjaKitty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!