-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
274 lines (248 loc) · 9.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import asyncio
import json
import logging
import os
import random
from datetime import datetime
import aiohttp
import asyncpg
import discord
import pyyoutube
import tmdbsimple as tmdb
from discord.ext import commands, tasks
import bot_secrets
import config
import views
from translator import TakoTranslator
from utils import clear_console
try:
import tomllib
except ModuleNotFoundError:
import tomli as tomllib # type: ignore
start_time = datetime.now()
reset = "\033[0m"
gray = "\033[90m"
green = "\033[92m"
blue = "\033[94m"
yellow = "\033[93m"
trimmer = f"{gray}----------{reset}"
tako_ascii_art = f"""
{yellow}
$$$$$$$$\ $$\
\__$$ __| $$ |
$$ | $$$$$$\ $$ | $$\ $$$$$$\
$$ | \____$$\ $$ | $$ |$$ __$$\
$$ | $$$$$$$ |$$$$$$ / $$ / $$ |
$$ |$$ __$$ |$$ _$$< $$ | $$ |
$$ |\$$$$$$$ |$$ | \$$\ \$$$$$$ |
\__| \_______|\__| \__| \______/
{reset}
"""
youtube_api = pyyoutube.Api(api_key=bot_secrets.YOUTUBE_API_KEY)
tmdb.API_KEY = bot_secrets.TMDB_API_KEY
class TakoBot(commands.AutoShardedBot):
async def on_ready(self):
if self.initialized:
return
print(f"\033[1F\033[2K🔓 {gray}|{reset} {yellow}Logged in as {self.user.name} (ID: {self.user.id}){reset}") # type: ignore
print(trimmer)
print(
f"{gray}>{reset} Startup took {round((datetime.now() - start_time).total_seconds(), 2)}s"
)
print(f"{gray}>{reset} Now running and listening to commands")
print(f"{gray}>{reset} Everything will be logged to discord.log")
print(f"{gray}>{reset} Press CTRL+C to exit")
print(trimmer)
self.initialized = True
async def setup_hook(self):
print(tako_ascii_art)
print(trimmer)
self.initialized = False
self.db_pool: asyncpg.Pool = await asyncpg.create_pool(
database=bot_secrets.DB_NAME,
host=bot_secrets.DB_HOST,
port=bot_secrets.DB_PORT if hasattr(bot_secrets, "DB_PORT") else 5432, # type: ignore
user=bot_secrets.DB_USER,
password=bot_secrets.DB_PASSWORD,
)
logger = logging.getLogger("startup")
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
formatter = logging.Formatter(
"{status} \033[90m|\033[0m {message}",
style="{",
)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.info(f"{blue}Loading cogs{reset}", extra={"status": "🔄"})
categories = 0
for category in os.listdir("cogs"):
categories += 1
await self.load_extension(f"cogs.{category}")
logger.info(
f"{green}Loaded {len(self.cogs)} cogs from {categories} categories{reset}",
extra={"status": "\033[1F\033[2K✅"},
)
logger.info(f"{blue}Loading i18n{reset}", extra={"status": "🔄"})
await self.tree.set_translator(TakoTranslator())
locales = []
for locale in os.listdir("i18n/misc"):
locales.append(locale.split(".")[0])
logger.info(f"{green}Loaded i18n{reset}", extra={"status": "\033[1F\033[2K✅"})
logger.info(
f"{green}Available locales ({len(locales)}): {', '.join(locales)}{reset}",
extra={"status": "✅"},
)
if hasattr(bot_secrets, "UPTIME_KUMA"):
self.uptime_kuma.start()
self.badges_update.start()
self.postgre_guilds = await self.db_pool.fetch("SELECT * FROM guilds") # type: ignore
self.update_phishing_list.start()
self.update_version.start()
self.presence_update.start()
await views.setup(self)
logger.info(f"{blue}Logging in...{reset}", extra={"status": f"{trimmer}\n🔄"})
@tasks.loop(seconds=55)
async def uptime_kuma(self):
async with aiohttp.ClientSession() as cs:
await cs.get(bot_secrets.UPTIME_KUMA + str(round(self.latency * 1000))) # type: ignore
return
@uptime_kuma.before_loop
async def before_uptime_kuma(self):
await self.wait_until_ready()
@tasks.loop(hours=1)
async def update_version(self):
with open("pyproject.toml", "rb") as f:
self.version = tomllib.load(f)["tool"]["commitizen"]["version"]
@tasks.loop(hours=1)
async def update_phishing_list(self):
self.sussy_domains = []
if hasattr(config, "ANTI_PHISHING_LIST"):
async with aiohttp.ClientSession() as cs:
for list in config.ANTI_PHISHING_LIST:
async with cs.get(list) as r:
r = await r.read()
r = json.loads(r)
self.sussy_domains.extend(r["domains"])
@tasks.loop(seconds=7.5)
async def presence_update(self):
presences = [
{
"name": f"over {len(self.guilds)} server{'s' if len(self.guilds) > 1 else ''}",
"type": discord.ActivityType.watching,
},
{
"name": f"{len(self.users)} user{'s' if len(self.users) > 1 else ''}",
"type": discord.ActivityType.listening,
},
{
"name": f"{len(self.tree.get_commands())} command{'s' if len(self.tree.get_commands()) > 1 else ''}",
"type": discord.ActivityType.listening,
},
{
"name": "/ commands",
"type": discord.ActivityType.listening,
},
{
"name": f"with version {self.version}",
"type": discord.ActivityType.playing,
},
{
"name": "translate at translate.tako-bot.com",
"type": discord.ActivityType.playing,
},
]
random_presence = random.choice(presences)
await self.change_presence(
activity=discord.Activity(
type=random_presence["type"], name=random_presence["name"]
)
)
@presence_update.before_loop
async def before_presence_update(self):
await self.wait_until_ready()
@tasks.loop(hours=1)
async def badges_update(self):
guild = None
guild = await self.fetch_guild(config.MAIN_GUILD, with_counts=False)
if not isinstance(guild, discord.Guild):
return
query = "UPDATE badges SET users = $1, emoji = $2 WHERE name = $3;"
for role in guild.roles:
if role.id == config.DONATOR_ROLE:
users = []
for member in role.members:
users.append(member.id)
await self.db_pool.execute(
query,
users,
"💖"
if not hasattr(config, "EMOJI_DONATOR") or not config.EMOJI_DONATOR
else config.EMOJI_DONATOR,
"donator",
)
continue
if role.id == config.TRANSLATOR_ROLE:
users = []
for member in role.members:
users.append(member.id)
await self.db_pool.execute(
query,
users,
"🌐"
if not hasattr(config, "EMOJI_TRANSLATOR")
or not config.EMOJI_TRANSLATOR
else config.EMOJI_TRANSLATOR,
"translator",
)
continue
if role.id == config.ALPHA_TESTER_ROLE:
users = []
for member in role.members:
users.append(member.id)
await self.db_pool.execute(
query,
users,
"🧪"
if not hasattr(config, "EMOJI_ALPHA_TESTER")
or not config.EMOJI_ALPHA_TESTER
else config.EMOJI_ALPHA_TESTER,
"alpha_tester",
)
continue
if role.id == config.DEV_ROLE:
users = []
for member in role.members:
users.append(member.id)
await self.db_pool.execute(
query,
users,
"💻"
if not hasattr(config, "EMOJI_DEV") or not config.EMOJI_DEV
else config.EMOJI_DEV,
"core_developer",
)
continue
@badges_update.before_loop
async def before_badges_update(self):
await self.wait_until_ready()
async def main():
clear_console()
logger = logging.getLogger("discord")
logger.setLevel(logging.INFO)
handler = logging.FileHandler(filename="discord.log", encoding="utf-8", mode="w")
formatter = logging.Formatter(
"{asctime} | {levelname: <8} | {module}:{funcName}:{lineno} - {message}",
style="{",
)
handler.setFormatter(formatter)
logger.addHandler(handler)
intents = discord.Intents.default()
intents.message_content = True
intents.guilds = True
intents.members = True
intents.reactions = True
bot: TakoBot = TakoBot(command_prefix="tk!", intents=intents)
await bot.start(bot_secrets.TOKEN)
if __name__ == "__main__":
asyncio.run(main())