-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
102 lines (86 loc) · 3.27 KB
/
main.py
File metadata and controls
102 lines (86 loc) · 3.27 KB
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
# -*- coding: UTF-8 -*-
import atexit
import sys
import traceback
from abc import ABC
import discord
import yaml
from discord.ext.commands.bot import Bot
from util import log
from util import update_guilds
# noinspection PyDunderSlots
class PixlBot(Bot, ABC):
config = None
ready = False
sentry = None
logger = None
# noinspection PyUnresolvedReferences
def __init__(self, bot_config: dict = None):
i = discord.Intents()
i.guilds = True
i.members = True
i.messages = True
i.message_content = True
self.loaded = False
super().__init__(bot_config["system"]["command_prefix"], intents=i)
self.config = bot_config
self.logger = log.init_logger("bot", bot_config["system"]["log_level"])
self.logger.info("Ohai! Initializing..")
self.atshutdown = []
update_guilds(bot_config["system"]["guilds"])
# Sentry.io integration
if "sentry" in self.config.keys():
import sentry_sdk
self.sentry = sentry_sdk
self.sentry.init(
self.config["sentry"]["init_url"], environment="production"
)
self.logger.warning("sentry.io integration enabled")
async def on_error(self, event, *args, **kwargs):
exc = sys.exc_info()
self.logger.error(f"{exc}: {event} -- {args} -- {kwargs}")
self.logger.error(f"{traceback.extract_tb(exc[2])}")
# Sentry.io integration
if self.sentry:
with self.sentry.configure_scope() as scope:
scope.set_tag("bot_event", event)
scope.set_extra("event_args", args)
scope.set_extra("event_kwargs", kwargs)
self.sentry.capture_exception(sys.exc_info())
async def on_connect(self):
self.logger.info("Connected to Discord")
if not self.loaded:
self.logger.info("Loading cogs..")
for ext in self.config["system"]["plugins"]:
try:
self.logger.info(f"Attempting to load {ext}")
self.load_extension(ext)
except Exception as e:
self.logger.error(e)
self.logger.error(f"Skipping {ext}")
continue
self.loaded = True
self.logger.info("Cog loading completed")
async def on_ready(self):
self.logger.info("Ready!")
self.logger.info("\n".join([f"{g.id}: {g.name}" for g in self.guilds]))
await self.sync_commands()
async def on_join_guild(self, guild):
self.logger.info(f"Invited to a guild: {guild}")
update_guilds(self.guilds)
async def on_guild_remove(self, guild):
self.logger.info(f"Removed from a guild: {guild}")
update_guilds(self.guilds)
# Seems to randomly trigger despite not actually being disconnected.
# async def on_disconnect(self):
# self.logger.warning("Disconnected!")
def shutdown(self):
self.logger.warning("Shutting down")
for f in self.atshutdown:
self.logger.debug("Executing shutdown triggers: ")
f()
with open("config.yml", "r") as file:
conf = yaml.safe_load(file)
bot = PixlBot(bot_config=conf)
atexit.register(bot.shutdown)
bot.run(conf["system"]["bot_token"])