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,
Solution 1:
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 = ""asyncdeffetch():
global last
global invites
await client.wait_until_ready()
gld = client.get_guild(int(guild_id))
logs = client.get_channel(int(logs_channel))
whileTrue:
invs = await gld.invites()
tmp = []
for i in invs:
for s in invites:
if s[0] == i.code:
ifint(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.eventasyncdefon_ready():
print("ready!")
await client.change_presence(activity = discord.Activity(name = "joins", type = 2))
@client.eventasyncdefon_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.
Solution 2:
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.
Solution 3:
Watching the number of uses an invite has had, or for when they run out of uses and are revoked, is the only way to see how a user was invited to the server.
Post a Comment for "Discord.py Show Who Invited A User"