This repository has been archived by the owner on Jun 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
155 lines (129 loc) · 5.23 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
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
from typing import Union
import discord
from discord import Message, Embed
from discord.ext import commands
from discord.ext.commands import AutoShardedBot, Context
from discord_slash import SlashContext, ComponentContext
import logging
import os
from datetime import datetime
from config import BotConfig
from utils.custom_slash_command import CustomSlashCommand
from utils import Counter, fakeDefer
from utils.errors import handle_error
from utils.game_data import GameDataServer
from utils.state_manger import StateManger, State
__author__ = "IanDesuyo"
__version__ = "3.1.9"
class AIKyaru(AutoShardedBot):
"""
A Discord bot for Princess Connect Re:Dive.
Kyaru best waifu <3
"""
def __init__(self):
self.__version__ = __version__
self.logger = logging.getLogger("AIKyaru")
self.logger.info("Starting...")
self.config = BotConfig()
self.gameData = GameDataServer()
self.counter = Counter()
self.stateManger = StateManger(self.config.get(["MONGODB_URL"]))
super().__init__(
command_prefix=commands.when_mentioned_or(self.config.get(["prefix"])),
case_insensitive=True,
help_command=None,
)
def init_(self):
"""
Load Cogs after :class:`SlashCommand` loaded.
Raises:
ExtensionAlreadyLoaded: Extension already loaded.
"""
for i in self.config.get(["cogs"]):
try:
self.load_extension(i)
self.logger.info(f"{i} loaded.")
except commands.ExtensionAlreadyLoaded:
self.logger.warning(f"{i} already loaded.")
except Exception as e:
self.logger.error(f"Error when loading {i}.")
raise e
async def on_ready(self):
if not hasattr(self, "uptime"):
# Only run once
self.uptime = datetime.utcnow()
await self.config.update_gacha_emojis(self)
await self.change_presence(
status=discord.Status.online,
activity=discord.Activity(
type=self.config.get(["activity", "type"]),
name=f"{self.config.get(['activity','prefix'])}{self.config.get(['activity','default_text'])}",
),
)
self.logger.info(f"Ready: {self.user} (ID: {self.user.id})")
async def on_shard_ready(self, shard_id: int):
self.logger.info(f"Shard ready, ID: {shard_id}")
async def on_shard_disconnect(self, shard_id: int):
self.logger.warning(f"Shard disconnected, ID: {shard_id}")
async def close(self):
for i in list(self.extensions):
self.unload_extension(i)
self.logger.info(f"{i} unloaded.")
await super().close()
async def send_debug(self, content: str = None, embed: Embed = None):
await self.get_channel(self.config.get(["DEBUG_CHANNEL"])).send(content=content, embed=embed)
if content:
self.logger.warning(f"Debug message sent: {content}")
elif embed:
self.logger.warning(f"Debug message sent: {embed.title}")
async def on_message(self, message: Message):
"""
Overwrite :func:`on_message` to inject :class:`State`.
"""
if message.author.bot:
return
self.counter.add("message_received")
ctx = await self.get_context(message)
if ctx.command:
ctx.state = State(self.stateManger, ctx)
ctx.defer = fakeDefer # Avoid errors when using defer()
self.counter.add(ctx.command.name)
await self.invoke(ctx)
async def invoke_slash_command(self, func, ctx: SlashContext, args):
"""
Overwrite :func:`slash_command` to inject :class:`State`.
"""
ctx.state = State(self.stateManger, ctx)
self.counter.add("slash_received")
self.counter.add("slash_" + ctx.name)
await func.invoke(ctx, **args)
async def invoke_component_callback(self, func, ctx: ComponentContext):
"""
Overwrite :func:`invoke_component_callback` to inject :class:`State`.
"""
ctx.state = State(self.stateManger, ctx)
self.counter.add("component_received")
self.counter.add("component_" + ctx.custom_id)
await func.invoke(ctx)
async def on_slash_command_error(self, ctx: SlashContext, error):
await handle_error(self, ctx, error)
async def on_component_callback_error(self, ctx: ComponentContext, error):
await handle_error(self, ctx, error)
async def on_command_error(self, ctx: Union[Context, SlashContext], error):
await handle_error(self, ctx, error)
if __name__ == "__main__":
log_handlers = [logging.StreamHandler()]
if not os.environ.get("NO_LOG_FILE"):
log_handlers.append(logging.FileHandler("bot.log"))
logging.basicConfig(
level=logging.INFO,
format="[%(levelname)s][%(asctime)s][%(name)s] %(message)s",
datefmt="%Y/%m/%d %H:%M:%S",
handlers=log_handlers,
)
for folder in ["./gameDB"]:
os.makedirs(folder, exist_ok=True)
bot = AIKyaru()
slash = CustomSlashCommand(bot, sync_commands=True, sync_on_cog_reload=False)
bot.init_()
bot.run(os.environ.get("BOT_TOKEN"))