-
Notifications
You must be signed in to change notification settings - Fork 23
/
invite_tracker.py
100 lines (88 loc) · 3.45 KB
/
invite_tracker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Copyright: GregTCLTK 2018-2021.
# Contact Developer on https://discord.gg/nPwjaJk (Skidder#8515 | 401817301919465482)
# Cog by: Quill (quillfires)
import discord
import asyncio
import json
import time
import typing
import datetime
from discord.ext import commands
# from discord.ext.commands import has_permissions
from discord import Embed
cfg = open("config.json", "r")
tmpconfig = cfg.read()
cfg.close()
config = json.loads(tmpconfig)
class invite_tracker(commands.Cog):
"""
Keep track of your invites
"""
def __init__(self, bot):
self.bot = bot
self.logs_channel = config["logs-channel-id"]
self.version = "1.0.0"
self.invites = {}
bot.loop.create_task(self.load())
async def load(self):
await self.bot.wait_until_ready()
# load the invites
for guild in self.bot.guilds:
try:
self.invites[guild.id] = await guild.invites()
except:
pass
def find_invite_by_code(self, inv_list, code):
for inv in inv_list:
if inv.code == code:
return inv
@commands.Cog.listener()
async def on_member_join(self, member):
logs = self.bot.get_channel(int(self.logs_channel))
eme = Embed(description="Just joined the server", color=0x03d692, title=" ")
eme.set_author(name=str(member), icon_url=member.avatar_url)
eme.set_footer(text="ID: " + str(member.id))
eme.timestamp = member.joined_at
try:
invs_before = self.invites[member.guild.id]
invs_after = await member.guild.invites()
self.invites[member.guild.id] = invs_after
for invite in invs_before:
if invite.uses < self.find_invite_by_code(invs_after, invite.code).uses:
eme.add_field(name="Used invite",
value=f"Inviter: {invite.inviter.mention} (`{invite.inviter}` | `{str(invite.inviter.id)}`)\nCode: `{invite.code}`\nUses: ` {str(invite.uses)} `", inline=False)
except:
pass
await logs.send(embed=eme)
@commands.Cog.listener()
async def on_member_remove(self, member):
logs = self.bot.get_channel(int(self.logs_channel))
eme = Embed(description="Just left the server", color=0xff0000, title=" ")
eme.set_author(name=str(member), icon_url=member.avatar_url)
eme.set_footer(text="ID: " + str(member.id))
eme.timestamp = member.joined_at
try:
invs_before = self.invites[member.guild.id]
invs_after = await member.guild.invites()
self.invites[member.guild.id] = invs_after
for invite in invs_before:
if invite.uses > self.find_invite_by_code(invs_after, invite.code).uses:
eme.add_field(name="Used invite",
value=f"Inviter: {invite.inviter.mention} (`{invite.inviter}` | `{str(invite.inviter.id)}`)\nCode: `{invite.code}`\nUses: ` {str(invite.uses)} `", inline=False)
except:
pass
await logs.send(embed=eme)
@commands.Cog.listener()
async def on_guild_join(self, guild):
try:
self.invites[guild.id] = await guild.invites()
except:
pass
@commands.Cog.listener()
async def on_guild_remove(self, guild):
try:
self.invites.pop(guild.id)
except:
pass
def setup(bot):
bot.add_cog(invite_tracker(bot))