-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
496 lines (453 loc) · 16.7 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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
import asyncio
import os
import re
import time
import disnake
from Levenshtein import ratio
from disnake import Intents
from disnake.ext import commands
from disnake.ui import Button, View
from dotenv import load_dotenv
import Embeds
import constants
import database
import utils
from other_functions import setup
from Paginate import CreatePaginator
"""
Rebuild card database with API call
"""
setup()
"""
Initializing bot permissions and global variables, loading token and startup confirmation message.
"""
in_game = False
uptime = int(time.time())
intents = Intents.default()
intents.message_content = True
bot = commands.InteractionBot(intents=intents)
load_dotenv()
token = os.getenv("DISCORD_TOKEN")
@bot.event
async def on_ready():
print("Ready to draw.")
@bot.slash_command(name="draw", description="Draw 2 cards, just like Pot of Greed.")
async def draw(ctx):
title = "Pot of Greed activates!"
embed = Embeds.DrawEmbedBuilder(title=title)
button = Button(
label="Draw again",
style=disnake.ButtonStyle.green,
emoji=disnake.utils.get(ctx.guild.emojis, name="cards"),
)
async def redraw(interaction):
new_embed = Embeds.DrawEmbedBuilder(title=title)
await interaction.response.edit_message(
embed=new_embed, file=new_embed.file, view=view
)
view = View()
view.add_item(button)
button.callback = redraw
await ctx.send(embed=embed, file=embed.file, view=view)
@bot.slash_command(name="info", description="Some info about me.")
async def info(ctx):
"""
Sends a simple embed with information about the bot, such as the name of his creator (me) and his uptime, as well
as some tips for the user.
Parameters:
----------
:param ctx: The invocation context of the command.
"""
global uptime
title = "My name is Bot of Greed!"
description = """I am a Discord bot created by [Pedro Silva](https://github.com/pedrosilva17), \
that uses the [YGOPRODeck Database](https://db.ygoprodeck.com/) API to populate its database.
**Author's note**: if you have any issues with the bot or would like to contribute to the project, \
click my name above to go to my Github page, where you can find my socials and the repository for \
this bot. Consider leaving a star on the repo if you enjoy it!
"""
file = disnake.File("profile.png", filename="profile.png")
embed = Embeds.InfoEmbedBuilder(
title=title, description=description, file=file, time=uptime
)
await ctx.send(embed=embed, file=file)
@bot.slash_command(
name="guess",
description="Given a card's text or artwork, guess its name. You have 90 seconds per game to guess.",
)
async def guess(
ctx,
mode: str = commands.Param(
name="mode",
description="The game mode to play.",
choices=["Card Text", "Artwork"],
),
):
"""
Guessing game that tests the user's ability to identify a card based on its description or artwork, depending on the
value of the "mode" parameter.
Parameters:
----------
:param ctx: The invocation context of the command.
:param mode: The game mode to play:
- "Card Text": Guess the card through its description.
- "Artwork": Guess the card through its artwork.
"""
"""
global in_game
if in_game:
await ctx.send("Hey, there's still a game in progress! Type 'quit' if you want to end it and then "
"start another.", ephemeral=True)
return
"""
await ctx.response.defer()
loading_emoji = disnake.utils.get(ctx.guild.emojis, name="level_spin")
await ctx.send(f"Preparing the game... {loading_emoji}")
title = "Guess the card!"
if mode == "Card Text":
thumb = disnake.File("artworks/artwork_81210420.jpg", "thumbnail.jpg")
embed = Embeds.GuessEmbedBuilder(
title=title, color=constants.hat_purple, file=thumb, mode=mode
)
else:
embed = Embeds.GuessEmbedBuilder(
title=title, color=constants.hat_purple, mode=mode
)
card = embed.card
file = embed.file
try:
button = Button(
label="Hint",
style=disnake.ButtonStyle.green,
emoji=constants.emojis["red_question_mark"],
)
view = View()
view.add_item(button)
async def show_hint(inter):
"""
Upon clicking the "Hint" button, show the first letter of each word that composes the card name, as
well as the number of letters, on the footer of the embed displaying the game.
Parameters:
----------
:param inter: The interaction invoking this function (the click on the button).
"""
hint = ""
hint_list = card["name"].split()
for word in hint_list:
hint += word[0] + re.sub("[A-z]", "_ ", word[1:]) + " "
embed.set_footer(text=f"Hint: {hint} | Type: {card['type']}")
await inter.response.edit_message(embed=embed, view=view)
return
async def game_loop():
"""
Loop that controls the flow of the game. There are 5 lives shared by every member that decides to guess,
and if they are all spent, the game ends. Guessing the card name or typing "quit" produces the same result.
"""
global in_game
in_game = True
tries = 5
while in_game:
answer = await bot.wait_for("message")
if (
not answer.author.bot
and answer.channel == ctx.channel
and answer.author.id == ctx.author.id
):
if card["name"].lower() == answer.content.lower():
await answer.reply(
f"{answer.author.mention} got it right! Well done."
)
in_game = False
break
match answer.content.lower():
case "quit":
await answer.reply(
f"{answer.author.mention} ended the game. The answer is {card['name']}."
)
in_game = False
case _:
tries -= 1
match tries:
case 1:
await ctx.send(
f"{answer.author.mention}, wrong! Last chance..."
)
case 0:
await ctx.send(
f"Game over! The answer is {card['name']}."
)
in_game = False
case _:
await ctx.send(
f"{answer.author.mention}, wrong! {tries} tries left."
)
await asyncio.sleep(0.3)
button.callback = show_hint
await ctx.edit_original_message(content="", embed=embed, view=view, file=file)
await asyncio.sleep(0.3)
await asyncio.wait_for(game_loop(), timeout=90)
except asyncio.TimeoutError:
await ctx.send(f"Time's up! The answer is {card['name']}.")
in_game = False
return
NAMES = database.card_attribute_list("name")
BETA_NAMES = database.card_attribute_list("beta_name")
TYPES = database.card_attribute_list("type")
SUBTYPES = database.card_attribute_list("subtype")
ARCHETYPES = database.card_attribute_list("archetype")
async def autocomp_names(inter: disnake.ApplicationCommandInteraction, _input: str):
_input = re.escape(_input)
if _input.strip() == "":
return []
result = sorted(
[name for name in NAMES],
key=lambda s: ratio(s, _input) + utils.count_whole_words(_input, s.split(" ")),
reverse=True,
)
return result[:25]
async def autocomp_beta_names(
inter: disnake.ApplicationCommandInteraction, _input: str
):
_input = re.escape(_input)
if _input.strip() == "":
return []
result = sorted(
[name for name in BETA_NAMES],
key=lambda s: ratio(s, _input) + utils.count_whole_words(_input, s.split(" ")),
reverse=True,
)
return result[:25]
async def autocomp_types(inter: disnake.ApplicationCommandInteraction, _input: str):
_input = re.escape(_input)
if _input.strip() == "":
return []
result = [card_type for card_type in TYPES if _input.lower() in card_type.lower()]
return result[:25]
async def autocomp_subtypes(inter: disnake.ApplicationCommandInteraction, _input: str):
_input = re.escape(_input)
if _input.strip() == "":
return []
result = [subtype for subtype in SUBTYPES if _input.lower() in subtype.lower()]
return result[:25]
async def autocomp_archetypes(
inter: disnake.ApplicationCommandInteraction, _input: str
):
_input = re.escape(_input)
if _input.strip() == "":
return []
arch_list = set()
for archetype in ARCHETYPES:
arch_list.update(archetype.split(", "))
result = sorted(
[archetype for archetype in arch_list],
key=lambda s: ratio(s, _input) + utils.count_whole_words(_input, s.split(" ")),
reverse=True,
)
return result[:25]
@bot.slash_command(
name="search",
description="Search for cards using multiple filters.",
)
async def search(
ctx,
name: str = commands.Param(
default=None,
name="name",
description="Filter by name using autocomplete to select one card.",
autocomplete=autocomp_names,
),
beta_name: str = commands.Param(
default=None,
name="beta_name",
description="Filter by beta (old) name (one card).",
autocomplete=autocomp_beta_names,
),
name_partial: str = commands.Param(
default=None,
name="name_partial",
description="Filter by name with partial string matching.",
),
archetype: str = commands.Param(
default=None,
name="archetype",
description="Filter by card archetype.",
autocomplete=autocomp_archetypes,
),
card_type: str = commands.Param(
default=None,
name="type",
description="Filter by card type.",
autocomplete=autocomp_types,
),
subtype: str = commands.Param(
default=None,
name="subtype",
description="Filter by card subtype.",
autocomplete=autocomp_subtypes,
),
attribute: str = commands.Param(
default=None,
name="attribute",
description="Filter by card attribute.",
choices=database.card_attribute_list("attribute"),
),
attack: str = commands.Param(
default=None, name="attack", description="Filter by attack points."
),
defense: str = commands.Param(
default=None, name="defense", description="Filter by defense points."
),
level: str = commands.Param(
default=None, name="level", description="Filter by level."
),
rank: str = commands.Param(
default=None, name="rank", description="Filter by rank."
),
scale: str = commands.Param(
default=None, name="scale", description="Filter by Pendulum Scale value."
),
link: str = commands.Param(
default=None, name="link", description="Filter by link value."
),
link_marker: str = commands.Param(
default=None, name="link_markers", description="Filter by link markers."
),
sort: str = commands.Param(
default="Name",
name="sort_by",
description="Sort the results in case more than one card is found. "
"Defaults to name.",
choices=list(constants.short_terms.keys())[:-2],
),
order: str = commands.Param(
default="ASC",
name="order",
description="Choose sorting order. Defaults to ascending order.",
choices=["ASC", "DESC"],
),
):
"""
Searches the database for cards, using a variety of filters and sorting options, and displays the results
in a user-friendly and appealing manner, using an embed.
Parameters:
----------
:param ctx: The invocation context of the command.
:param name: Name to filter with, using autocomplete with fuzzy string matching to select one card.
:param beta_name: Old/Beta name to filter with.
:param name_partial: Name to filter with, with partial string matching to select one or more cards.
:param archetype: Card archetype to filter with (not always accurate, some cards are missing their archetypes).
:param card_type: Card type to filter with.
:param subtype: Card "sybtype" to filter with.
:param attribute: Card attribute to filter with.
:param attack: Attack value to filter with.
:param defense: Defense value to filter with.
:param level: Level value to filter with.
:param rank: Rank value to filter with.
:param link: Link value to filter with.
:param link_marker: Link marker(s) to filter with.
:param scale: Pendulum scale value to filter with.
:param sort: Parameter to sort by. Defaults to name.
:param order: Order to sort by (ascending/descending). Defaults to ascending order.
"""
await ctx.response.defer()
parameters = {
"name": name if name else ("PARTIAL" + name_partial) if name_partial else None,
"beta_name": beta_name,
"archetype": archetype,
"type": card_type,
"subtype": subtype,
"attribute": attribute,
"atk": attack,
"def": defense,
"level": level,
"rank": rank,
"scale": scale,
"linkval": link,
"linkmarkers": link_marker,
"sort": sort,
"order": order,
}
response = database.query({k: v for k, v in parameters.items() if v is not None})
# embed, file = utils.embed_builder(ctx=ctx, embed_type="Search", response=response, subtype="Card")
embed = utils.search_wrapper(ctx, response)
if type(embed) != list:
await ctx.send(embed=embed, file=embed.file)
else:
view = CreatePaginator(
ctx=ctx, embeds=embed, author=ctx.author.id, size=len(embed)
)
await ctx.send(embed=embed[0], view=view)
return
@bot.slash_command(
name="guide",
description="Get help regarding less straightforward commands.",
)
async def guide(
ctx,
command: str = commands.Param(
name="command",
description="The command to be checked.",
choices=["Guess", "Search"],
),
):
"""
Sends embed(s) with more detailed explanations of the chosen command in the "command" parameter.
Parameters:
----------
:param ctx: The invocation context of the command.
:param command: The command to display more information about.
"""
match command:
case "Guess":
title = "Minigame: Guess"
help_text = open("help/guess_help.txt", "r").read().rstrip("\n")
file = disnake.File(
"artworks/artwork_81210420.jpg", filename="help_image.jpg"
)
embed = Embeds.HelpEmbedBuilder(
title=title,
description=help_text,
color=constants.hat_purple,
file=file,
)
await ctx.send(embed=embed, file=file)
return
case "Search":
title = "Command: Search"
file = disnake.File(
"artworks/artwork_74191528.jpg", filename="help_image.jpg"
)
embeds = []
page_count = 2
for page_num in range(1, 3):
help_text = (
open(f"help/search_help_{page_num}.txt", "r").read().rstrip("\n")
)
embed = Embeds.HelpEmbedBuilder(
title=title,
description=help_text,
color=constants.fate_blue,
file=file,
page_num=page_num,
page_count=page_count,
)
embeds.append(embed)
author = ctx.author.id
view = CreatePaginator(
ctx=ctx,
embeds=embeds,
author=author,
size=len(embeds),
has_dropdown=False,
)
await ctx.send(embed=embeds[0], file=file, view=view)
return
@bot.slash_command(name="test", description="This is a test.")
async def test_embed(ctx):
global uptime
file = disnake.File("profile.png", filename="profile.png")
embed = Embeds.InfoEmbedBuilder(
title="Test", description="This is a test.", time=uptime, file=file
)
await ctx.send(embed=embed, file=file)
bot.run(token)