-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.py
425 lines (368 loc) · 21.5 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import json
import logging
import sqlite3
import traceback
import discord.errors
from discord import app_commands
import nest_asyncio
from bot.apis_clients.discord import Discord_token
from bot.core.slash_commands.army_link_analyze import army_link_analyze
from bot.core.slash_commands.auto_roles import auto_roles_bh, auto_roles_leagues, auto_roles_th
from bot.core.slash_commands.bot_creators_only.add_a_bot_id import add_a_bot_id
from bot.core.slash_commands.bot_creators_only.add_reaction_with_id import add_reaction_with_id
from bot.core.slash_commands.bot_creators_only.delete_messages import delete_message
from bot.core.slash_commands.bot_creators_only.download_emojis import download_emojis
from bot.core.slash_commands.bot_creators_only.find_user_by_id import find_user_by_id
from bot.core.slash_commands.bot_creators_only.reboot import reboot
from bot.core.slash_commands.bot_creators_only.refresh_dbl import refresh_dbl
from bot.core.slash_commands.bot_creators_only.servers_list import servers_list
from bot.core.slash_commands.bot_creators_only.stats import stats
from bot.core.slash_commands.bot_info import bot_info
from bot.core.slash_commands.buildings_bh import buildings_bh
from bot.core.slash_commands.buildings_th import buildings_th
from bot.core.slash_commands.clan_donations import clan_donations
from bot.core.slash_commands.clan_info import clan_info
from bot.core.slash_commands.clan_members import clan_members
from bot.core.slash_commands.clan_super_troops import clan_super_troops
from bot.core.slash_commands.clan_current_war import clan_current_war
from bot.core.slash_commands.credits import credits
from bot.core.slash_commands.help import help
from bot.core.slash_commands.link_coc_account import link_coc_account, unlink_coc_account
from bot.core.slash_commands.player_info import player_info
from bot.core.slash_commands.search_clan import search_clan
from bot.core.slash_commands.user_info import user_info
from bot.functions import *
from data.config import Config
from data.useful import Ids
nest_asyncio.apply()
votes_file = open(f"{Config['secure_folder_path']}votes.json", "r")
Votes = json.load(votes_file)
if __name__ == "__main__":
from bot.apis_clients.discord import Clash_info
connection_modifiable = sqlite3.connect(f"{Config['secure_folder_path']}secure.db")
cursor_modifiable = connection_modifiable.cursor()
async def check_cmd_perms(interaction: discord.Interaction, command: str = None):
if interaction.channel.type is discord.ChannelType.private:
await interaction.response.send_message("Slash commands are not available in direct messages. Please add the bot to your server and then use slash commands there.")
return -1
if command is None:
command = interaction.data["name"]
if command == "_help":
command = "help"
from data.required_permissions import Required_permissions
permissions_needed = Required_permissions[command]
missing_bot_perms = []
for perm in permissions_needed:
if not getattr(interaction.app_permissions, perm):
missing_bot_perms.append(perm)
if missing_bot_perms:
text = "the bot doesn't have the permission(s):"
for perm in missing_bot_perms:
text += f"\n{perm}"
if len(missing_bot_perms) == 1:
text += "\nPlease grant it to the bot and send again the command."
else:
text += "\nPlease grant them to the bot and send again the command."
await interaction.response.send_message(f"Missing permissions: {text}")
return -1
return True
def edit_commands_used(user_id: int, cmd: str):
text = f"""INSERT INTO bot_usage(user_id) SELECT({user_id}) WHERE NOT EXISTS(SELECT 1 FROM bot_usage WHERE user_id={user_id})"""
cursor_modifiable.execute(text)
text = f"""UPDATE bot_usage SET {cmd} = (SELECT {cmd} FROM bot_usage WHERE user_id={user_id})+1 WHERE user_id={user_id}"""
cursor_modifiable.execute(text)
connection_modifiable.commit()
command_tree = app_commands.CommandTree(Clash_info)
async def on_error(interaction: discord.Interaction, error: discord.app_commands.AppCommandError):
if type(error) is discord.app_commands.CommandInvokeError and type(error.original) is discord.errors.NotFound:
if interaction.app_permissions.send_messages:
await interaction.channel.send("The command has expired, please try again\n\n*This message will be deleted in 15 seconds*", delete_after=15)
else:
print(traceback.format_exc())
return
command_tree.on_error = on_error
@command_tree.command(name="help", description="Show the help message to use @Clash INFO#3976")
async def _help(interaction: discord.Interaction):
if await check_cmd_perms(interaction) == -1:
return
await help(interaction)
edit_commands_used(interaction.user.id, "help")
return
@command_tree.command(name="_help", description="Show the help message to use @Clash INFO#3976")
async def __help(interaction: discord.Interaction):
if await check_cmd_perms(interaction) == -1:
return
await help(interaction)
edit_commands_used(interaction.user.id, "help")
return
# CLASH OF CLANS
@command_tree.command(name="army_link_analyze", description="Show the troops and spells from an in-game army link")
@app_commands.describe(army_link="Army link, gettable from Clash of Clans > Army > Quick Train > Share > Share as link")
async def _army_link_analyze(interaction: discord.Interaction, army_link: str):
if await check_cmd_perms(interaction) == -1:
return
await army_link_analyze(interaction, army_link)
edit_commands_used(interaction.user.id, "army_link_analyze")
return
@command_tree.command(name="auto_roles_bh", description="[Administrators only] Create an auto-roles system to give the BH level roles")
@app_commands.guild_only()
@app_commands.describe(channel="Channel where it will be the auto-roles system")
@app_commands.default_permissions(administrator=True)
async def _auto_roles_bh(interaction: discord.Interaction, channel: discord.TextChannel = None):
if await check_cmd_perms(interaction) == -1:
return
if channel is None:
channel = interaction.channel
await auto_roles_bh(interaction, channel)
edit_commands_used(interaction.user.id, "auto_roles_bh")
return
@command_tree.command(name="auto_roles_leagues", description="[Administrators only] Create an auto-roles system to give the league role")
@app_commands.guild_only()
@app_commands.describe(channel="Channel where it will be the auto-roles system")
@app_commands.default_permissions(administrator=True)
async def _auto_roles_leagues(interaction: discord.Interaction, channel: discord.TextChannel = None):
if await check_cmd_perms(interaction) == -1:
return
if channel is None:
channel = interaction.channel
await auto_roles_leagues(interaction, channel)
edit_commands_used(interaction.user.id, "auto_roles_leagues")
return
@command_tree.command(name="auto_roles_th", description="[Administrators only] Create an auto-roles system to give the TH level roles")
@app_commands.guild_only()
@app_commands.describe(channel="Channel where it will be the auto-roles system")
@app_commands.default_permissions(administrator=True)
async def _auto_roles_th(interaction: discord.Interaction, channel: discord.TextChannel = None):
if await check_cmd_perms(interaction) == -1:
return
if channel is None:
channel = interaction.channel
await auto_roles_th(interaction, channel)
edit_commands_used(interaction.user.id, "auto_roles_th")
return
@command_tree.command(name="buildings_bh", description="Show the maximum level for each buildings at the given Builder Hall level")
@app_commands.describe(builder_hall_level="Builder Hall level")
async def _buildings_bh(interaction: discord.Interaction, builder_hall_level: int = 0):
if await check_cmd_perms(interaction) == -1:
return
await buildings_bh(interaction, builder_hall_level)
edit_commands_used(interaction.user.id, "buildings_bh")
return
@command_tree.command(name="buildings_th", description="Show the maximum level for each buildings at the given Town Hall level")
@app_commands.describe(town_hall_level="Town Hall level")
async def _buildings_th(interaction: discord.Interaction, town_hall_level: int = 0):
if await check_cmd_perms(interaction) == -1:
return
await buildings_th(interaction, town_hall_level)
edit_commands_used(interaction.user.id, "buildings_th")
return
@command_tree.command(name="clan_donations", description="Show the clan members, sorted by donations stats")
@app_commands.describe(clan_tag="Clash of Clans clan tag, format: #A1B2C3D4")
async def _clan_donations(interaction: discord.Interaction, clan_tag: str):
if await check_cmd_perms(interaction) == -1:
return
await clan_donations(interaction, clan_tag)
edit_commands_used(interaction.user.id, "clan_donations")
return
@command_tree.command(name="clan_info", description="Show data about the clan")
@app_commands.describe(clan_tag="Clash of Clans clan tag, format: #A1B2C3D4")
async def _clan_info(interaction: discord.Interaction, clan_tag: str):
if await check_cmd_perms(interaction) == -1:
return
await clan_info(interaction, clan_tag)
edit_commands_used(interaction.user.id, "clan_info")
return
@command_tree.command(name="clan_members", description="Show the clan members")
@app_commands.describe(clan_tag="Clash of Clans clan tag, format: #A1B2C3D4")
async def _clan_members(interaction: discord.Interaction, clan_tag: str):
if await check_cmd_perms(interaction) == -1:
return
await clan_members(interaction, clan_tag)
edit_commands_used(interaction.user.id, "clan_members")
return
@command_tree.command(name="clan_current_war", description="Show data about the clan war")
@app_commands.describe(clan_tag="Clash of Clans clan tag, format: #A1B2C3D4")
async def _clan_current_war(interaction: discord.Interaction, clan_tag: str):
if await check_cmd_perms(interaction) == -1:
return
await clan_current_war(interaction, clan_tag)
edit_commands_used(interaction.user.id, "clan_current_war")
return
@command_tree.command(name="player_info", description="Show data about the player")
@app_commands.describe(player_tag="Clash of Clans player tag, format: #A1B2C3D4")
@app_commands.describe(information="Information wanted")
@app_commands.choices(information=[
app_commands.Choice(name="main", value="main"),
app_commands.Choice(name="troops", value="troops"),
app_commands.Choice(name="success", value="success")
])
async def _player_info(interaction: discord.Interaction, player_tag: str, information: app_commands.Choice[str]):
if await check_cmd_perms(interaction) == -1:
return
await player_info(interaction, player_tag, information.value)
edit_commands_used(interaction.user.id, "player_info")
return
@command_tree.command(name="search_clan", description="Search clans by name")
@app_commands.describe(name="Clan name")
async def _search_clan(interaction: discord.Interaction, name: str):
if await check_cmd_perms(interaction) == -1:
return
await search_clan(interaction, name)
edit_commands_used(interaction.user.id, "search_clan")
return
@command_tree.command(name="clan_super_troops", description="Show which super troop has been activated, and by which player of the clan")
@app_commands.describe(clan_tag="Clash of Clans clan tag, format: #A1B2C3D4")
async def _clan_super_troops(interaction: discord.Interaction, clan_tag: str):
if await check_cmd_perms(interaction) == -1:
return
await clan_super_troops(interaction, clan_tag)
edit_commands_used(interaction.user.id, "clan_super_troops")
return
@command_tree.command(name="link_coc_account", description="Link your Clash of Clans account to your Discord account")
@app_commands.describe(player_tag="Clash of Clans player tag, format: #A1B2C3D4")
@app_commands.describe(api_token="Your API token, findable in Clash of Clans > Settings > More Settings > API Token > Show")
async def _link_coc_account(interaction: discord.Interaction, player_tag: str, api_token: str):
if await check_cmd_perms(interaction) == -1:
return
await link_coc_account(interaction, player_tag, api_token)
edit_commands_used(interaction.user.id, "link_coc_account")
return
@command_tree.command(name="unlink_coc_account", description="Unlink your Clash of Clans account from your Discord account")
@app_commands.describe(player_tag="Clash of Clans player tag, format: #A1B2C3D4")
async def _unlink_coc_account(interaction: discord.Interaction, player_tag: str):
if await check_cmd_perms(interaction) == -1:
return
await unlink_coc_account(interaction, player_tag)
edit_commands_used(interaction.user.id, "unlink_coc_account")
return
@command_tree.command(name="user_info", description="Show permissions, when the user joined Discord / the server and their avatar")
@app_commands.describe(user="The member")
async def _user_info(interaction: discord.Interaction, user: discord.User):
if await check_cmd_perms(interaction) == -1:
return
await user_info(interaction, user)
edit_commands_used(interaction.user.id, "user_info")
return
# OTHER
@command_tree.command(name="bot_info", description="Show some information about the bot")
async def _bot_info(interaction: discord.Interaction):
if await check_cmd_perms(interaction) == -1:
return
await bot_info(interaction)
edit_commands_used(interaction.user.id, "bot_info")
return
@command_tree.command(name="credits", description="Show bot credits")
async def _credits(interaction: discord.Interaction):
if await check_cmd_perms(interaction) == -1:
return
await credits(interaction)
edit_commands_used(interaction.user.id, "credits")
return
# CREATORS
@command_tree.command(name="__add_a_bot_id", guild=discord.Object(id=Ids["Bot_creators_only_server"]), description="Add the bot with the given id")
async def ___add_a_bot_id(interaction: discord.Interaction, bot_id: str):
await add_a_bot_id(interaction, int(bot_id))
return
@command_tree.command(name="__add_reaction_with_id", guild=discord.Object(id=Ids["Bot_creators_only_server"]), description="Add a reaction everywhere with the channel/message/emoji ids")
async def ___add_reaction_with_id(interaction: discord.Interaction, channel_id: str, message_id: str, emoji_id: str):
await add_reaction_with_id(interaction, int(channel_id), int(message_id), int(emoji_id))
return
@command_tree.command(name="__delete_messages", guild=discord.Object(id=Ids["Bot_creators_only_server"]), description="Delete messages quickly")
async def ___delete_messages(interaction: discord.Interaction, channel_id: str, number: int):
await delete_message(interaction, int(channel_id), number)
return
@command_tree.command(name="__download_emojis", guild=discord.Object(id=Ids["Bot_creators_only_server"]), description="Send a .zip message with the emojis of emojis channels")
@app_commands.choices(recreate_emojis_zip=[
app_commands.Choice(name="True", value=1),
app_commands.Choice(name="False", value=0)
])
async def ___download_emojis(interaction: discord.Interaction, recreate_emojis_zip: app_commands.Choice[int]):
await download_emojis(interaction, bool(recreate_emojis_zip.value))
return
@command_tree.command(name="__find_user_by_id", guild=discord.Object(id=Ids["Bot_creators_only_server"]), description="Find the user with the given id")
async def ___find_user_by_id(interaction: discord.Interaction, user_id: str):
await find_user_by_id(interaction, int(user_id))
return
@command_tree.command(name="__reboot", guild=discord.Object(id=Ids["Bot_creators_only_server"]), description="Reboot the Raspberry Pi")
async def ___reboot(interaction: discord.Interaction):
await reboot(interaction)
return
@command_tree.command(name="__refresh_dbl", guild=discord.Object(id=Ids["Bot_creators_only_server"]), description="Refresh the top.gg servers counter")
async def ___refresh_dbl(interaction: discord.Interaction):
await refresh_dbl(interaction)
return
@command_tree.command(name="__servers_list", guild=discord.Object(id=Ids["Bot_creators_only_server"]), description="Show all the servers with the bot")
async def ___servers_list(interaction: discord.Interaction):
await servers_list(interaction)
return
@command_tree.command(name="__stats", guild=discord.Object(id=Ids["Bot_creators_only_server"]), description="Show monthly usages")
async def ___stats(interaction: discord.Interaction):
await stats(interaction)
return
@Clash_info.event
async def on_interaction(interaction: discord.Interaction):
if interaction.type is discord.InteractionType.component:
try:
if interaction.message.embeds[0].footer.text == "joined_guild_message":
from bot.core.components.buttons.joined_guild_message import joined_guild_message
await joined_guild_message(interaction)
return
footer_text = interaction.message.embeds[0].footer.text
command_name = footer_text.split("|")[0]
if command_name == "auto_roles_bh" or command_name == "auto_roles bh":
if await check_cmd_perms(interaction, command="auto_roles_bh") == -1:
return
from bot.core.components.select_menus.auto_roles import auto_roles_bh
await auto_roles_bh(interaction)
return
elif command_name == "auto_roles_leagues" or command_name == "auto_roles_league" or command_name == "auto_roles league":
if await check_cmd_perms(interaction, command="auto_roles_leagues") == -1:
return
from bot.core.components.select_menus.auto_roles import auto_roles_leagues
await auto_roles_leagues(interaction)
return
elif command_name == "auto_roles_th" or command_name == "auto_roles th":
if await check_cmd_perms(interaction, command="auto_roles_th") == -1:
return
from bot.core.components.select_menus.auto_roles import auto_roles_th
await auto_roles_th(interaction)
return
if interaction.user.id == int(footer_text.split("|")[-1]):
if command_name == "buildings_bh":
if await check_cmd_perms(interaction, command="buildings_bh") == -1:
return
from bot.core.components.select_menus.change_bh_lvl import change_bh_lvl
await change_bh_lvl(interaction)
return
elif command_name == "buildings_th":
if await check_cmd_perms(interaction, command="buildings_th") == -1:
return
from bot.core.components.select_menus.change_th_lvl import change_th_lvl
await change_th_lvl(interaction)
return
elif command_name == "search_clan":
if await check_cmd_perms(interaction, command="search_clan") == -1:
return
from bot.core.components.select_menus.change_search_clan import change_search_clan
await change_search_clan(interaction)
return
elif command_name == "player_info":
if await check_cmd_perms(interaction, command="player_info") == -1:
return
from bot.core.components.select_menus.change_player_info_page import change_player_stats_page
await change_player_stats_page(interaction)
return
else:
await interaction.response.send_message("You can only use select menu of slash commands sent by you", ephemeral=True)
except discord.errors.NotFound:
await interaction.channel.send("The command has expired, please try again\n\n*This message will be deleted in 15 seconds*", delete_after=15)
return
async def sync_commands():
await command_tree.sync()
await command_tree.sync(guild=discord.Object(id=Ids["Bot_creators_only_server"]))
Clash_info.sync_commands = sync_commands
while True:
try:
Clash_info.run(Discord_token, log_handler=logging.StreamHandler(), log_level=logging.INFO)
except Exception as e:
print(f"\n\nError : {e}\n\n")