-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
173 lines (125 loc) · 3.77 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import asyncio
import discord
from discord.ext import commands
from discord_slash import SlashCommand
import os, sys
import argparse
os.environ["LOG_LEVEL"] = "WARNING"
from cogs.client import client
from cogs.controller import Controller
from cogs.utils import *
from cogs.api.database import database
from loguru import logger
# create `/tmp`
if not os.path.exists("tmp"):
os.makedirs("tmp")
# initialize
TOKEN = open("tmp/token.txt", "r").read()
cogs = [
"cogs.controller",
"cogs.misc",
"cogs.eval",
"cogs.error",
]
slash = SlashCommand(client, sync_commands=True, sync_on_cog_reload=True)
@client.event
async def on_ready():
# print bot user and discrim on the console
print("{0.user}".format(client))
# change the presence
await client.change_presence(
status=discord.Status.online,
)
# sync commands
logger.debug("Syncing commands")
try:
await slash.sync_all_commands()
logger.debug("Synced commands")
except:
logger.debug("Could not sync commands")
# setup all feeds
await client.get_cog("Controller").on_ready()
@client.event
async def on_member_join(member):
pass
@client.event
async def on_message(message):
# embed meta
embed = discord.Embed(color=0xF5F5F5)
source = "https://github.com/0x16c3/mitsu"
avatar = client.user.avatar_url
if message.content == "mitsu":
embed.set_author(name="Mitsu", url=source, icon_url=avatar)
embed.set_thumbnail(url=avatar)
embed.add_field(name="source", value=source, inline=False)
embed.set_footer(text="光")
await message.channel.send(embed=embed)
await client.process_commands(message)
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ("yes", "true", "t", "y", "1"):
return True
elif v.lower() in ("no", "false", "f", "n", "0"):
return False
else:
raise argparse.ArgumentTypeError("Boolean value expected.")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Process some integers.")
parser.add_argument(
"--debug",
dest="debug",
type=str2bool,
nargs="?",
default=False,
const=True,
help="enable debug mode",
)
parser.add_argument(
"--info",
dest="info",
type=str2bool,
nargs="?",
default=False,
const=True,
help="enable info mode",
)
args = parser.parse_args()
os.environ["LOG_LEVEL"] = "WARNING"
if args.info:
os.environ["LOG_LEVEL"] = "INFO"
elif args.debug:
os.environ["LOG_LEVEL"] = "DEBUG"
fmt = (
"<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green>"
" | "
"<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan>"
" | "
"<level>{level: <8}</level>: <level>{message}</level>"
)
logger.remove()
logger.add(sys.stderr, format=fmt, level=os.environ["LOG_LEVEL"])
logger.add("logs/log_{time}.log", enqueue=True, rotation="12:00")
for extension in cogs:
if args.debug and extension == "cogs.error":
continue
try:
client.load_extension(extension)
except Exception as error:
logger.error(f"{extension} could not be activated. [{error}]")
try:
client.load_extension("cogs.topgg")
except:
pass
async def update_roles(minutes: int):
while True:
await asyncio.sleep(minutes * 60)
logger.debug("Syncing commands")
await slash.sync_all_commands()
logger.debug("Synced commands")
try:
client.loop.create_task(client.get_cog("Controller").process())
client.loop.create_task(update_roles(minutes=3))
client.run(TOKEN)
except KeyboardInterrupt:
print("exit")