-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
69 lines (52 loc) · 2.16 KB
/
main.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
import logging, logging.handlers, typing
from pathlib import Path
from asyncio import run
from requests import post
from os import environ
import discord
import discord.ext.commands as cmds
from utils.help import CustomHelp
from utils.ids import *
logger = logging.getLogger(__name__)
# Good practices from discord example repository:
# https://github.com/Rapptz/discord.py/blob/v2.3.1/examples/advanced_startup.py
class CustomBot(cmds.Bot):
def __init__(self, *args, **kwargs):
super().__init__(
intents=discord.Intents.all(),
command_prefix="!",
help_command=CustomHelp(),
*args,
**kwargs)
self.debug_channel: discord.TextChannel
async def setup_hook(self):
cog_dir = Path("cogs")
for extension in cog_dir.iterdir():
if extension.is_file():
await self.load_extension(f"{cog_dir}.{extension.stem}")
logger.info(f"extension {extension.stem} loaded")
async def on_ready(self):
self.insalgo = typing.cast(discord.Guild, self.get_guild(INSALGO))
self.debug_channel = typing.cast(discord.TextChannel, self.get_channel(DEBUG))
await self.debug_channel.send("Up")
logger.info("bot up")
async def main():
# Terminal logger
logging.basicConfig(format="[%(levelname)s] %(name)s: %(message)s", level=logging.INFO)
# File logger
handler = logging.handlers.RotatingFileHandler(filename="bot.log", maxBytes=10**5, backupCount=5)
date_format = '%Y-%m-%d %H:%M:%S'
formatter = logging.Formatter('[{asctime}] [{levelname:<8}] {name}: {message}', date_format, style='{')
handler.setFormatter(formatter)
logging.getLogger().addHandler(handler)
async with CustomBot() as bot:
await bot.start(environ['DISCORD_TOKEN'])
if __name__ == "__main__":
try:
run(main())
except KeyboardInterrupt as kb_interrupt:
pass
# Sending a message to confirm shutdown :
headers = {'Authorization': 'Bot %s' % environ['DISCORD_TOKEN'] }
post(f"https://discord.com/api/v6/channels/{DEBUG}/messages", headers=headers, json={"content": "Down"})
#hehe