forked from andrewn6/alpha-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
100 lines (75 loc) · 2.55 KB
/
bot.py
File metadata and controls
100 lines (75 loc) · 2.55 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
"""
Alphabet Discord Bot
This bot helps manage all necessary administration and
automation for the Alphabet Discord server.
Author(s):
Pyro - https://github.com/Pyroseza
Agent - https://github.com/solidassassin
"""
from aiohttp import ClientSession
from discord.ext import commands
from pathlib import Path
from utils.config import Config
from utils.context import AlphaCtx
import logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s | %(levelname)s: %(name)s -> %(message)s",
datefmt="%H:%M:%S",
)
log = logging.getLogger(__name__)
class AlphaBot(commands.Bot):
"""
The main bot class where are the magic happens
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.config = Config().load()
async def on_ready(self):
log.info(f"{self.user} is in!")
async def get_context(self, message, *, cls=AlphaCtx):
return await super().get_context(message, cls=cls)
@property
def module_list(self):
m = list(Path("cogs").glob("*.py"))
all_cogs = [f"cogs.{i.name}"[:-3] for i in m]
return all_cogs
async def load_modules(self):
_output = []
for cog in self.module_list:
try:
self.load_extension(cog)
_output.append(f"[{cog}] loaded")
except commands.ExtensionAlreadyLoaded:
_output.append(f"[{cog}] already loaded")
except commands.ExtensionNotLoaded:
_output.append(f"[{cog}] not loaded")
log.exception(f"Failed to load {cog}")
return "\n".join(_output)
async def start(self, *args, **kwargs):
self.session = ClientSession()
await self.load_modules()
token = self.config.get("token")
if not token:
raise KeyError("Token not found in config!")
# await super().start(*args, **kwargs)
await super().start(token, *args, **kwargs)
async def close(self):
await self.session.close()
await super().close()
# create the alphabot instance
bot = AlphaBot(
command_prefix=commands.when_mentioned_or("alpha ", "Alpha "),
case_insensitive=True
)
@bot.event
async def on_ready():
main_id = bot.config.get('main_guild')
bot.main_guild = bot.get_guild(main_id) or bot.guilds[0]
print('\nActive in these guilds/servers:')
[print(g.name) for g in bot.guilds]
print('\nMain guild:', bot.main_guild.name)
print('\nAlphabot started successfully')
return True
bot.run()
print('Alphabot has terminated')