forked from sevenc-nanashi/rt-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
103 lines (83 loc) · 2.73 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
"""RT Backend (C) 2020 RT-Team
LICENSE : ./LICENSE
README : ./readme.md
"""
print("RT Discord Bot (C) 2020 RT-Team\nNow loading...")
from os import listdir
from sys import argv
from logging import handlers
import logging
import discord
from aiohttp import ClientSession
from ujson import load, dumps
from rtlib import RT, mysql, setup, websocket
from data import data, Colors
with open("auth.json", "r") as f:
secret = load(f)
# Botの準備を行う。
intents = discord.Intents.default()
intents.typing = False
intents.members = True
bot = RT(
data["prefixes"][argv[-1]], help_command=None, intents=intents,
allowed_mentions=discord.AllowedMentions(everyone=False, users=False),
activity=discord.Game("起動準備"), status=discord.Status.dnd
)
bot.secret = secret
bot.test = argv[-1] != "production"
if not bot.test:
websocket.WEBSOCKET_URI_BASE = "ws://146.59.153.178"
bot.data = data
bot.admins = data["admins"]
bot.owner_ids = data["admins"]
# 非推奨だがis_adminを作っておく。
bot.is_admin = bot.is_owner
bot.secret = secret
bot.mysql = bot.data["mysql"] = mysql.MySQLManager(
loop=bot.loop, **secret["mysql"], pool=True,
minsize=1, maxsize=500 if bot.test else 1000000, autocommit=True
)
bot.pool = bot.mysql.pool
bot.colors = data["colors"]
bot.Colors = Colors
bot._load = False
# 起動中だと教えられるようにするためのコグを読み込む。
bot.load_extension("cogs._first")
# スラッシュマネージャーを設定する。
bot.load_extension("rtlib.slash")
# onamiを読み込む。
bot.load_extension("onami")
@bot.listen()
async def on_ready():
bot.print("Connected to discord")
bot.session = ClientSession(loop=bot.loop, json_serialize=dumps)
bot.unload_extension("cogs._first")
# 拡張を読み込む。
setup(bot)
bot.load_extension("cogs._oldrole")
for name in listdir("cogs"):
if not name.startswith(("_", ".")):
try:
bot.load_extension(
f"cogs.{name[:-3] if name.endswith('.py') else name}"
)
except discord.ext.commands.NoEntryPointError as e:
if "setup" not in str(e):
raise e
else:
bot.print("[Extension]", "Loaded", name)
bot.print("Completed to boot RT")
bot.dispatch("full_ready")
bot._load = True
# loggingの準備をする。
logger = logging.getLogger('discord')
handler = handlers.RotatingFileHandler(
filename='log/discord.log', encoding='utf-8', mode='w',
maxBytes=10000000, backupCount=5
)
handler.setLevel(logging.DEBUG)
handler.setFormatter(logging.Formatter(
"[%(asctime)s][%(levelname)s][%(name)s] %(message)s"
))
logger.addHandler(handler)
bot.run(secret["token"][argv[-1]])