This repository has been archived by the owner on Feb 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.py
84 lines (62 loc) · 2.87 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
import discord
import os
import requests
import re
import emoji
import json
import aiohttp
from discord.ext import commands
from dotenv import load_dotenv
from helpers.config import VATADR_MEMBER_ROLE, VATSIM_MEMBER_ROLE, VATSIM_SUBDIVISION, GUILD_ID, BOT_TOKEN, REACTION_ROLES, REACTION_MESSAGE_IDS, REACTION_EMOJI, ROLE_REASONS, APPLICATION_ID, VATSIM_CHECK_MEMBER_URL, VATSIM_API_TOKEN
from helpers import config
load_dotenv('env')
class VATEye(commands.Bot):
def __init__(self):
super().__init__(
command_prefix="!",
intents=discord.Intents.all(),
application_id = APPLICATION_ID
)
async def setup_hook(self):
self.session = aiohttp.ClientSession()
await config.load_cogs(self)
await bot.tree.sync(guild = discord.Object(id = GUILD_ID))
async def on_ready(self) -> None:
print(f"Bot started. \nUsername: {self.user.name}. \nID: {self.user.id}\nDiscord Version: {discord.__version__}")
try:
await self.change_presence(activity=config.activity(), status=config.status())
except Exception as e:
print(f"Error changing presence. Exception: {e}")
async def on_member_update(self, before_update, user: discord.User):
if (before_update.nick == user.nick):
return
guild = self.get_guild(GUILD_ID)
vatadr_member = discord.utils.get(guild.roles, id=VATADR_MEMBER_ROLE)
vatsim_member = discord.utils.get(guild.roles, id=VATSIM_MEMBER_ROLE)
request = requests.get(VATSIM_CHECK_MEMBER_URL, headers={'Authorization': 'Token ' + VATSIM_API_TOKEN})
if request.status_code == requests.codes.ok:
data = request.json()
try:
cid = re.findall('\d+', str(user.nick))
if len(cid) < 1:
raise ValueError
should_have_vatadr = False
for item in data['results']:
if int(item['id']) == int(cid[0]) and str(item['subdivision']) == str(VATSIM_SUBDIVISION):
should_have_vatadr = True
if vatsim_member in user.roles:
if vatadr_member not in user.roles and should_have_vatadr == True:
await user.add_roles(vatadr_member)
elif vatadr_member in user.roles and should_have_vatadr == False:
await user.remove_roles(vatadr_member)
elif vatsim_member not in user.roles and vatadr_member in user.roles:
await user.remove_roles(vatadr_member)
except ValueError as e:
print(f"Tried to find an ID but it ther a ValueError, not found!")
except Exception as e:
print(f"{e}")
bot = VATEye()
try:
bot.run(BOT_TOKEN)
except Exception as e:
print(f"Error occured while starting the bot: {e}")