Last active
February 9, 2022 14:45
-
-
Save minibox24/f7fae61305ef118cf288bc4a9957af41 to your computer and use it in GitHub Desktop.
Discord Button Akinator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
@minibox24 | |
λΌμ΄μ μ€ κ΄λ ¨ μλ΄: | |
Source λ§ν¬ λ²νΌμ μμ νμ§ λ§μΈμ. | |
""" | |
import discord | |
from discord.ext.commands import Bot | |
from discord_components import DiscordComponents, Button, ButtonStyle | |
from discord_components.interaction import InteractionType | |
from akinator.async_aki import Akinator | |
from asyncio import TimeoutError | |
from uuid import uuid4 | |
bot = Bot(command_prefix="`") | |
choose_label = { | |
"en": ["yes", "probably", "idk", "probably not", "no", "Back"], | |
"kr": ["μ", "κ·Έλ΄κ²λλ€", "λͺ¨λ₯΄κ² μ΅λλ€", "μλκ²λλ€", "μλμ", "λμκ°κΈ°"], | |
} | |
def make_id(uuid: str, name: str): | |
return f"{uuid}::{name}" | |
def get_id(key: str, uuid: str): | |
splited = key.split("::") | |
if len(splited) != 2: | |
return | |
if splited[0] != uuid: | |
return | |
return splited[1] | |
@bot.event | |
async def on_ready(): | |
DiscordComponents(bot) | |
print(f"Logged in as {bot.user}!") | |
@bot.command() | |
async def aki(ctx): | |
uuid = uuid4().hex | |
aki = Akinator() | |
aki.mode = "en" | |
block = False | |
embed = discord.Embed( | |
title="Discord Akinator", | |
description="Choose options", | |
color=discord.Color.blurple(), | |
) | |
embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar_url) | |
embed.set_footer(text="made by Minibox#3332") | |
message = await ctx.send( | |
embed=embed, | |
components=[ | |
[ | |
Button( | |
label="Start!", | |
emoji="π", | |
style=ButtonStyle.green, | |
id=make_id(uuid, "start"), | |
), | |
Button( | |
label="Exit", | |
emoji="πͺ", | |
style=ButtonStyle.red, | |
id=make_id(uuid, "exit"), | |
), | |
Button( | |
label="Source", style=ButtonStyle.URL, url="https://gist.github.com/minibox24/f7fae61305ef118cf288bc4a9957af41" | |
), | |
], | |
[ | |
Button( | |
label="English", | |
emoji="πΊπΈ", | |
style=ButtonStyle.blue, | |
id=make_id(uuid, "en"), | |
disabled=True, | |
), | |
Button( | |
label="Animals", | |
emoji="πΆ", | |
style=ButtonStyle.gray, | |
id=make_id(uuid, "en_animals"), | |
), | |
Button( | |
label="Objects", | |
emoji="π»", | |
style=ButtonStyle.gray, | |
id=make_id(uuid, "en_objects"), | |
), | |
Button( | |
label="νκ΅μ΄", | |
emoji="π°π·", | |
style=ButtonStyle.gray, | |
id=make_id(uuid, "kr"), | |
), | |
], | |
], | |
) | |
try: | |
while True: | |
try: | |
interaction = await bot.wait_for( | |
"button_click", | |
check=lambda i: i.component.id.startswith(uuid), | |
timeout=60 * 5, | |
) | |
except TimeoutError: | |
return await message.delete() | |
if interaction.user != ctx.author: | |
await interaction.respond( | |
content="This is not your game. if you want to play send ``aki`", | |
ephemeral=True, | |
) | |
continue | |
await interaction.respond(type=InteractionType.DeferredUpdateMessage) | |
action_id = get_id(interaction.component.id, uuid) | |
# Game Start | |
if action_id == "start": | |
if block: | |
continue | |
block = True | |
q = await aki.start_game(language=aki.mode) | |
block = False | |
lang = "kr" if aki.mode == "kr" else "en" | |
embed = interaction.message.embeds[0] | |
embed.description = q | |
await message.edit( | |
embed=embed, | |
components=[ | |
[ | |
Button( | |
label=choose_label[lang][0], | |
emoji="β ", | |
style=ButtonStyle.green, | |
id=make_id(uuid, "y"), | |
), | |
Button( | |
label=choose_label[lang][1], | |
emoji="π", | |
style=ButtonStyle.blue, | |
id=make_id(uuid, "p"), | |
), | |
Button( | |
label=choose_label[lang][2], | |
emoji="π€", | |
style=ButtonStyle.gray, | |
id=make_id(uuid, "idk"), | |
), | |
Button( | |
label=choose_label[lang][3], | |
emoji="π", | |
style=ButtonStyle.blue, | |
id=make_id(uuid, "pn"), | |
), | |
Button( | |
label=choose_label[lang][4], | |
emoji="β", | |
style=ButtonStyle.red, | |
id=make_id(uuid, "n"), | |
), | |
], | |
[ | |
Button( | |
label=choose_label[lang][5], | |
emoji="β¬ οΈ", | |
style=ButtonStyle.green, | |
id=make_id(uuid, "back"), | |
), | |
Button( | |
label="Source", | |
style=ButtonStyle.URL, | |
url="https://gist.github.com/minibox24/f7fae61305ef118cf288bc4a9957af41", | |
), | |
Button( | |
label=f"Progress :: {aki.progression}%", | |
emoji="π‘", | |
style=ButtonStyle.gray, | |
id=make_id(uuid, "progress"), | |
disabled=True, | |
), | |
Button( | |
label="Exit", | |
emoji="πͺ", | |
style=ButtonStyle.red, | |
id=make_id(uuid, "exit"), | |
), | |
], | |
], | |
) | |
# Game Exit | |
if action_id == "exit": | |
return await message.delete() | |
# Modes | |
if action_id in ["en", "en_animals", "en_objects", "kr"]: | |
aki.mode = action_id | |
components = interaction.message.components | |
for component in components[3:]: | |
component.style = ButtonStyle.gray | |
component.disabled = False | |
if action_id == get_id(component.id, uuid): | |
component.disabled = True | |
component.style = ButtonStyle.blue | |
await message.edit(components=[components[:3], components[3:]]) | |
# Process | |
async def process_message(q): | |
embed = interaction.message.embeds[0] | |
embed.description = q | |
components = interaction.message.components | |
for component in components: | |
if component.id == make_id(uuid, "progress"): | |
component.label = f"Progress :: {round(aki.progression, 2)}%" | |
await message.edit( | |
embed=embed, components=[components[:5], components[5:]] | |
) | |
# Play | |
if action_id in ["y", "p", "idk", "pn", "n"]: | |
if block: | |
continue | |
block = True | |
q = await aki.answer(action_id) | |
block = False | |
if aki.progression >= 85: | |
await aki.win() | |
name = aki.first_guess["name"] | |
desc = aki.first_guess["description"] | |
img = aki.first_guess["absolute_picture_path"] | |
embed = interaction.message.embeds[0] | |
embed.description = f"**`{name}`**\n\n{desc}" | |
embed.set_image(url=img) | |
await message.edit( | |
embed=embed, | |
components=[ | |
Button( | |
label="Source", | |
style=ButtonStyle.URL, | |
url="https://gist.github.com/minibox24/f7fae61305ef118cf288bc4a9957af41", | |
), | |
], | |
) | |
return | |
await process_message(q) | |
# Back | |
if action_id == "back": | |
if block: | |
continue | |
block = True | |
q = await aki.back() | |
block = False | |
await process_message(q) | |
except Exception: | |
await message.edit(content="β οΈ", components=[]) | |
bot.run("your token") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment