This repository has been archived by the owner on Sep 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
executable file
·166 lines (143 loc) · 4.66 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
import discord
from discord.ext import commands, tasks
import aiohttp
import asyncio
import os
from dotenv import load_dotenv
from lib import utils
from logging import basicConfig, getLogger, INFO
#load .env
load_dotenv()
#environ JISHAKU
os.environ["JISHAKU_NO_UNDERSCORE"] = "True"
os.environ["JISHAKU_NO_DM_TRACEBACK"] = "True"
os.environ["JISHAKU_ALWAYS_DM_TRACEBACK"] = "False"
class TsumugiChan(commands.AutoShardedBot):
def __init__(self, *args, **kwargs) -> None:
self.logger = kwargs.get("logger", None)
self.session = kwargs.get("session", None)
super().__init__(*args, **kwargs)
self.instance = {
"shards": {},
"ready": False,
"session": 0,
"exception": {
"error": None,
"traceback": None,
"context": None
},
"channels": {
"presence": 997358448968740985,
}
}
self._cogs = [
"cogs.mido_admins", "cogs.mido_events", "cogs.mido_slash",
"cogs.mido_slash_admin", "cogs.mido_music", "cogs.mido_music_slash",
"cogs.mido_punish", "jishaku"
]
#post
async def post_api(self, status: int=2):
d = {
"identity": "tsumugi",
"status": status
}
try:
async with self.session.request(
"POST",
"https://api.midorichan.cf/v1/service/status",
headers={"Authorization": f"Bearer {os.environ['MIDORI_TOKEN']}"},
json=d
) as request:
data = await discord.http.json_or_text(request)
if request.status == 200:
self.logger.info(f"API: Updated service status - {data}")
else:
self.logger.warning(f"API: Service status update failed - {data}")
except Exception as exc:
self.logger.warning(f"ERROR: {exc}")
#api status poster
@tasks.loop(minutes=15.0)
async def api_status_poster(self):
if self.instance["ready"]:
await self.post_api(2)
else:
await self.post_api(1)
#overwrite start
async def start(self) -> None:
token = os.environ.get("TOKEN", None)
if not token:
self.logger.critical(
"RUNNER: Bot token was not provided"
)
return
else:
try:
await super().start(token)
except Exception as exc:
self.logger.critical(f"RUNNER: {exc}")
else:
self.logger.info(
"RUNNER: Enabling tsumugi discordbot..."
)
#close
async def close(self):
self.logger.info("RUNNER: Disabling tsumugi discordbot...")
await self.post_api(0)
try:
await super().close()
except Exception as exc:
self.logger.error(f"RUNNER: Failed to close Client : {exc}")
#setup_hook
async def setup_hook(self):
self.logger.info("SYSTEM: Setting up...")
for i in self._cogs:
try:
await self.load_extension(i)
except Exception as exc:
self.logger.error(f"ERROR: {exc}")
else:
self.logger.info(f"SYSTEM: Cog {i} load")
try:
self.api_status_poster.start()
except Exception as exc:
self.logger.error(f"ERROR: {exc}")
#on_ready
async def on_ready(self) -> None:
self.logger.info(f"RUNNER: Logged in as {self.user}")
await self.post_api(1)
try:
await self.change_presence(
status=discord.Status.online,
activity=discord.Game(
"のんびりお茶ちう...><"
)
)
except Exception as exc:
self.logger.error(exc)
else:
self.logger.info("SYSTEM: Presence changed")
self.instance["ready"] = True
self.logger.info("RUNNER: Enabled tsumugi discordbot")
#run
async def main():
#logger
basicConfig(
level=INFO,
format="%(asctime)s - %(name)s - [%(levelname)s]: %(message)s"
)
logger = getLogger("discord")
#vars
intents = discord.Intents.all()
#run
async with aiohttp.ClientSession() as session:
async with TsumugiChan(
logger=logger,
intents=intents,
command_prefix=os.environ.get("PREFIX", "."),
shard_count=2,
status=discord.Status.idle,
session=session
) as bot:
await bot.start()
if __name__ == "__main__":
asyncio.run(main())