-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.py
171 lines (142 loc) · 4.83 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
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
166
167
168
169
170
171
#
# This file is part of Orbyt. (https://github.com/nxmrqlly/orbyt)
# Copyright (c) 2023-present Ritam Das
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""Boilerplate code for Bot's root functionalities"""
import logging
import sys
import discord
import jishaku
import asqlite
from discord.ext import commands
from termcolor import colored
from logging.handlers import RotatingFileHandler
from exts.util.text_format import spaced_padding, CustomFormatter
from config import DEBUG, PROD_TOKEN, DEBUG_BOT_TOKEN
INITIAL_EXTENSIONS = [
"exts.info",
"exts.dev",
"exts.tags",
"exts.festive",
"exts.embed",
"exts.error",
]
class Orbyt(commands.AutoShardedBot):
"""Base Class for the bot"""
def __init__(self, *args, **kwargs):
intents = discord.Intents.default()
intents.members = True
super().__init__(
command_prefix=commands.when_mentioned,
case_insensitive=True,
strip_after_prefix=True,
intents=discord.Intents.all() if DEBUG else intents,
owner_id=767115163127906334,
activity=discord.Activity(
type=discord.ActivityType.custom,
name="Orbyt",
state="💫 Exploring new dimensions. /ping",
),
status="idle",
*args,
**kwargs,
)
self._codeblock = "```"
@property
def db(self):
return self.pool
async def setup_hook(self):
"""Set up logger and load extensions"""
## ----- Logging ----- ##
logger = logging.getLogger("discord")
logger.setLevel(logging.INFO)
fmt = "[{asctime}] [{levelname}] - {name}: {message}"
date_fmt = "%H:%M:%S"
# Log to file
f_formatter = logging.Formatter(fmt, date_fmt, "{")
file_handler = RotatingFileHandler(
"./logs/discord.log",
mode="w",
encoding="utf-8",
maxBytes=5 * 1024 * 1024,
)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(f_formatter)
# Log to console
c_formatter = CustomFormatter(fmt, date_fmt, "{")
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
console_handler.setFormatter(c_formatter)
# Add & Finish up
logger.addHandler(file_handler)
logger.addHandler(console_handler)
## ----- Database Setup ----- ##
self.pool = await asqlite.create_pool("./db/orbyt.db")
async with self.pool.acquire() as c:
with open("./db/schema.sql") as f:
await c.executescript(f.read())
## ----- Load Extensions ----- ##
await self.load_extension("jishaku")
loaded_exts = []
for ext in INITIAL_EXTENSIONS:
try:
await self.load_extension(ext)
except commands.ExtensionError as exc:
print(colored(f"Failed to load extension {ext}: {exc}", "red"))
else:
loaded_exts.append(ext)
print(
colored(
spaced_padding("Extensions", 52)
+ "\n| > "
+ "\n| > ".join(loaded_exts)
+ "\n",
"light_blue",
)
)
async def close(self):
await self.pool.close()
await super().close()
async def on_ready(self):
"""Called when the bot is ready"""
basic_info = [
f"{tag:<12}: {value}"
for tag, value in [
("User", self.user),
("ID", self.user.id),
("Python", sys.version),
("Discord.py", discord.__version__),
("Jishaku", jishaku.__version__),
("Guilds", len(self.guilds)),
("Shards", self.shard_count),
("Debug Mode", DEBUG),
]
]
print(
colored(
"\n"
+ spaced_padding("Logged In", 52)
+ "\n| > "
+ "\n| > ".join(basic_info)
+ "\n",
"cyan",
)
)
async def start(self) -> None:
"""Asyncrously start the bot"""
await super().start(
token=DEBUG_BOT_TOKEN if DEBUG else PROD_TOKEN,
reconnect=True,
)