-
Notifications
You must be signed in to change notification settings - Fork 7
/
run.py
165 lines (137 loc) · 6.45 KB
/
run.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# -*- coding: utf-8 -*-
"""
MIT License
Copyright (c) 2019-2020 Arthur
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from configparser import ConfigParser
from datetime import datetime
from distutils.util import strtobool
from glob import glob
from os import name, execv, system, environ
from sys import argv, executable, stdout, exit
from discord import LoginFailure, Status, Activity, ActivityType, Intents
from utilsx.console import Prettier, Colors
from utilsx.discord import BotX
from utils import VersionHandler, PrintHandler
# Check if the operating system is linux or windows. (nt = windows)
# If its windows, change the console clear command and the filepath delimiter.
clear, back_slash = "clear", "/"
if name == "nt":
clear, back_slash = "cls", "\\"
# Read our configuration
cfg = ConfigParser()
cfg.read("./config/config.cfg")
if list(cfg) == ["DEFAULT"]:
msg = "No valid config was loaded! (did you forget to change the names in the config folder?)"
raise RuntimeError(msg)
class Bot(BotX):
"""
The main bot object, this contains our handlers and loads our extensions
"""
def __init__(self, _p: Prettier, _ph: PrintHandler):
super().__init__(Intents().all())
system(clear)
stdout.flush()
self.prettier = _p
self.ph = _ph
self.ph.info("Initializing client...")
self.cfg = cfg
self.prefix = cfg["BOT"].get("prefix", "!")
self.vm = VersionHandler()
if strtobool(cfg["UPDATER"].get("enabled", "true")):
self.check_for_updates()
self.ph.info("No updates found, starting bot...")
self.ph.info("Started loading extensions.")
@staticmethod
def restart():
system(clear)
stdout.flush()
execv(executable, ['python'] + argv)
def check_for_updates(self):
self.ph.info("Checking for updates...")
if not self.vm.is_latest:
self.ph.info("Update found! Started updating bot to the latest version...")
self.vm.update_version()
self.ph.info("Successfully updated to the latest version. Rebooting bot.")
self.restart()
async def on_ready(self):
from extensions.ReactionLogger import ReactionLogger
from extensions.ReactionRoles import ReactionRoles
extensions = [ReactionRoles]
if strtobool(cfg["REACTION_LOGGING"].get("enabled", "true")):
extensions.append(ReactionLogger)
for index, extension in enumerate(extensions):
await self.add_cog(extension(self))
if strtobool(cfg["CONSOLE"].get("print_imports", "true")):
self.ph.info(f"Successfully loaded "
f"{Colors.light_magenta.value + extension.__name__}")
self.ph.info(f"Currently running on v{self.vm.version}!")
print()
async def get_correct_bot_object(path: str, objects: list):
value = cfg["BOT"].get(path, None)
if not value:
self.ph.warn("Presence path could not be found!")
self.ph.fatal("Invalid config file.")
raise SystemExit(1)
for obj in objects:
if obj[0] == value.strip().lower():
return obj[1]
self.ph.warn(f"Could not parse given configuration for value '{path}' ['{value}' was given]")
self.ph.fatal("Invalid config file.")
raise SystemExit(1)
statuses = [("online", Status.online), ("dnd", Status.dnd), ("idle", Status.idle),
("invisible", Status.invisible)]
try:
if strtobool(cfg["BOT"].get("rich_presence_enabled")):
activities = [("playing", ActivityType.playing), ("streaming", ActivityType.streaming),
("watching", ActivityType.watching), ("listening to", ActivityType.listening)]
await self.change_presence(
activity=Activity(type=await get_correct_bot_object("rich_presence_type", activities),
name=cfg["BOT"].get("rich_presence", "Invalid configuration"),
start=datetime.now(),
url="https://www.twitch.tv/bel_justice"),
status=await get_correct_bot_object("bot_status", statuses)
)
else:
await self.change_presence(status=await get_correct_bot_object("bot_status", statuses))
except SystemExit as err:
await self.close()
raise err
if __name__ == "__main__":
prettier = Prettier(colors_enabled=strtobool(cfg["CONSOLE"].get("colors", "true")),
auto_strip_message=True)
ph = PrintHandler(prettier, strtobool(cfg["CONSOLE"].get("print_log", "true")))
token = None
if strtobool(cfg["TOKEN"].get("token_env_enabled", "false")):
location = cfg["TOKEN"].get("token_env", "RR_BOT_TOKEN")
try:
token = environ[location]
except KeyError as e:
ph.fatal("The requested environment variable doesn't exist.\n"
"Please check if you have reopened your CLI or of it is set.")
exit(1)
else:
token = cfg["TOKEN"]["token"]
try:
Bot(prettier, ph).run(token)
except LoginFailure:
ph.fatal("A wrong bot token was provided!")
except SystemExit:
ph.fatal("Shutting down bot, caused by previous warning or fatal.")
except Exception as e:
ph.fatal(f"Shutting down bot, error:\n{e}")