-
Notifications
You must be signed in to change notification settings - Fork 4
/
bot.py
114 lines (97 loc) · 3.79 KB
/
bot.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import json
import discord
from discord.ext import commands
from discord.utils import get
from idena_auth.auth import auth
with open("bot_config.json") as f:
CONFIG = json.load(f)
with open(CONFIG["status_file"]) as f:
STATUS = json.load(f)
ROLES = {"": "Logged", "Candidate": "Logged", "Newbie": "Newbie", "Verified": "Verified", "Human": "Human", "Suspended": "Suspended", "Zombie": "Zombie"}
PREFIX = "auth/"
bot = commands.Bot(command_prefix=PREFIX)
async def remove_roles(user, is_id=False):
if is_id:
user_id = user
else:
user_id = user.id
for guild in bot.guilds:
member = guild.get_member(user_id)
if member is not None:
await member.remove_roles(*[get(guild.roles, name=role) for _, role in ROLES.items()])
@bot.command()
async def update_roles(ctx):
"""Admin command"""
if ctx.message.author.id not in CONFIG["admins"]:
return
await update_guild_roles(bot.guilds)
await ctx.message.add_reaction("👍")
async def update_guild_roles(guilds, check_last_status=True):
global STATUS
data = auth.db.get_all_status()
with open(CONFIG["status_file"]) as f:
STATUS = json.load(f)
for user_id, address, status in data:
state = ""
if address in STATUS:
state = STATUS[address]
if check_last_status and state == status:
continue
auth.db.set_address_status(address, state)
for guild in guilds:
member = guild.get_member(int(user_id))
if member is not None:
await member.remove_roles(get(guild.roles, name=ROLES[status]))
await member.add_roles(get(guild.roles, name=ROLES[state]))
@bot.command()
async def login(ctx):
"""Gives a link to login"""
if auth.db.is_token_auth(str(ctx.message.author.id)):
address = auth.db.get_address(str(ctx.message.author.id))
await ctx.message.author.send("You are already logged with {} ({})\nYou can logout using `!logout`".format(address, auth.db.get_address_status(address)))
return
auth.get_dna_url(token=str(ctx.message.author.id))
await ctx.message.author.send("Login with {}{}\nThen update your status with `{}status`".format("https://idenauth.dragginator.com/auth/", ctx.message.author.id, PREFIX))
await ctx.message.add_reaction("👍")
@bot.command()
async def status(ctx):
"""Updates your status after you logged in"""
if not auth.db.is_token_auth(str(ctx.message.author.id)):
await login(ctx)
return
await remove_roles(ctx.message.author)
user_id = ctx.message.author.id
address = auth.db.get_address(str(user_id))
state = ""
if address in STATUS:
state = STATUS[address]
auth.db.set_address_status(address, status=state)
for guild in bot.guilds:
member = guild.get_member(user_id)
if member is not None:
await member.add_roles(get(guild.roles, name=ROLES[state]))
await ctx.message.author.send("Your status has been updated to '{}'".format(ROLES[state]))
await ctx.message.add_reaction("👍")
@bot.command()
async def logout(ctx):
"""Unlinks your address from this account"""
auth.db.remove_token(str(ctx.message.author.id))
await remove_roles(ctx.message.author)
await ctx.message.add_reaction("👍")
@bot.event
async def on_guild_join(guild):
for _, role in ROLES.items():
await guild.create_role(name=role)
await update_guild_roles([guild], check_last_status=False)
@bot.event
async def on_member_join(member):
status = auth.db.get_token_status(member.id)
if status is None:
return
await member.add_roles(get(member.guild.roles, name=ROLES[status]))
"""
@bot.event
async def on_ready():
await remove_roles(, is_id=True)
"""
bot.run(CONFIG["token"])