-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
120 lines (100 loc) · 3.16 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
import discord
import copy
import aiohttp
import datetime
from discord.ext import commands, store_true
from data.manager import DataManager
import logging
import sys
import asyncpg
import re
ignore = [
"name",
"levelno",
"pathname",
"filename",
"module",
"exc_info",
"exc_text",
"stack_info",
"lineno",
"funcName",
"created",
"msecs",
"relativeCreated",
"thread",
"threadName",
"processName",
"process",
]
class LoggingFormat(logging.Formatter):
def format(self, record):
arguments = {
key: value for key, value in record.__dict__.items() if key not in ignore
}
return ", ".join(f"{key}={value}" for key, value in arguments.items())
async def prefix(bot, message):
if not message.guild:
return commands.when_mentioned_or(bot.config.prefix)(bot, message)
return commands.when_mentioned_or(
await bot.get_cog("Meta").get_prefix(message.channel.guild)
)(bot, message)
class Pokeland(store_true.StoreTrueMixin, commands.Bot):
"""The custom subclass for the bot"""
def __init__(self, config=None):
if not config:
self.config = __import__("config").config
self.logger = logging.getLogger("discord")
self.logger.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(LoggingFormat())
self.logger.addHandler(handler)
self.data = DataManager(self)
super().__init__(
command_prefix=prefix, case_insensitive=True, strip_after_prefix=True
)
self.add_check(
commands.bot_has_permissions(
send_messages=True,
view_channel=True,
read_message_history=True,
add_reactions=True,
embed_links=True,
attach_files=True,
use_external_emojis=True,
).predicate
)
self._uptime = discord.utils.utcnow()
self.loop.run_until_complete(self.setup())
@property
def db(self):
return self.get_cog("Database")
@property
def uptime(self):
return discord.utils.format_dt(self._uptime, "R")
async def get_context(self, message, cls=None):
cls = cls or self.context
return await super().get_context(message, cls=cls)
async def setup(self):
self.session = aiohttp.ClientSession()
self.connection = await asyncpg.create_pool(self.config.db_string)
for extension in self.config.extensions:
self.load_extension(extension)
async def on_ready(self):
self.logger.info(f"Logged in as {self.user}")
async def on_command(self, ctx):
self.logger.info(
"Command Ran",
extra={
"id": ctx.author.id,
"tag": str(ctx.author),
"guild_id": ctx.guild.id if ctx.guild else None,
"guild": str(ctx.guild),
"content": ctx.message.content,
},
)
def run(self):
super().run(self.config.token)
async def close(self):
await self.session.close()
await super().close()