-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aki.py
144 lines (128 loc) · 8.25 KB
/
aki.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
import discord
from discord.ext import commands
from discord import app_commands
from aki_new import Akinator, CantGoBackAnyFurther
from util_discord import command_check, description_helper, get_guild_prefix
categories = ['people', 'objects', 'animals']
languages = ['en', 'ar', 'cn', 'de', 'es', 'fr', 'it', 'jp', 'kr', 'nl', 'pl', 'pt', 'ru', 'tr', 'id']
def w(ctx: commands.Context, aki: Akinator) -> discord.Embed:
embed_win = discord.Embed(title=aki.name_proposition, description=aki.description_proposition,
colour=0x00FF00)
if ctx.author.avatar: embed_win.set_author(name=ctx.author, icon_url=ctx.author.avatar.url)
else: embed_win.set_author(name=ctx.author)
embed_win.set_image(url=aki.photo)
# embed_win.add_field(name="Ranking", value="#"+aki.first_guess['ranking'], inline=True)
embed_win.add_field(name="Questions", value=aki.step+1, inline=True)
embed_win.add_field(name="Progress", value=f"{aki.progression}%", inline=True)
return embed_win
def qEmbed(aki: Akinator, ctx: commands.Context) -> discord.Embed:
e = discord.Embed(title=f"{aki.step+1}. {aki.question}", description=f"{aki.progression}%", color=0x00FF00)
if ctx.author.avatar: e.set_author(name=ctx.author, icon_url=ctx.author.avatar.url)
else: e.set_author(name=ctx.author)
return e
class QView(discord.ui.View):
def __init__(self, aki: Akinator, ctx: commands.Context):
super().__init__(timeout=None)
self.add_item(ButtonAction(aki, ctx, 0, 'Yes', '✅', 'y'))
self.add_item(ButtonAction(aki, ctx, 0, 'No', '❌', 'n'))
self.add_item(ButtonAction(aki, ctx, 0, 'Don\'t Know', '❓', 'idk'))
self.add_item(ButtonAction(aki, ctx, 1, 'Probably', '👍', 'p'))
self.add_item(ButtonAction(aki, ctx, 1, 'Probably Not', '👎', 'pn'))
self.add_item(ButtonAction(aki, ctx, 2, 'Back', '⏮', 'b'))
self.add_item(ButtonAction(aki, ctx, 2, 'Stop', '🛑', 's'))
class ButtonAction(discord.ui.Button):
def __init__(self, aki: Akinator, ctx: commands.Context, row: int, l: str, emoji: str, action: str):
super().__init__(label=l, style=discord.ButtonStyle.success, emoji=emoji, row=row)
self.aki, self.action, self.ctx = aki, action, ctx
async def callback(self, interaction: discord.Interaction):
if interaction.user != self.ctx.author:
return await interaction.response.send_message(content=f"<@{self.ctx.author.id}> is playing this game! Use `{await get_guild_prefix(self.ctx)}aki` to create your own game.",
ephemeral=True)
if self.action == 's':
return await interaction.response.edit_message(content=f"Skill issue <@{interaction.user.id}>", view=None, embed=None)
if self.action == 'b':
try:
await interaction.response.defer()
await self.aki.back()
except CantGoBackAnyFurther:
return await interaction.followup.send(content=f"CantGoBackAnyFurther", ephemeral=True)
else:
await interaction.response.edit_message(view=None)
await self.aki.answer(self.action)
if not self.aki.win and self.aki.step < 79:
await interaction.edit_original_response(embed=qEmbed(self.aki, self.ctx), view=QView(self.aki, self.ctx))
else:
embed = w(self.ctx, self.aki)
await interaction.edit_original_response(embed=embed, view=RView(self.aki, self.ctx))
class RView(discord.ui.View):
def __init__(self, aki: Akinator, ctx):
super().__init__(timeout=None)
self.add_item(ButtonAction0(aki, ctx, 'Yes', '✅', 'y'))
self.add_item(ButtonAction0(aki, ctx, 'No', '❌', 'n'))
class ButtonAction0(discord.ui.Button):
def __init__(self, aki: Akinator, ctx: commands.Context, l: str, emoji: str, action: str):
super().__init__(label=l, style=discord.ButtonStyle.success, emoji=emoji)
self.aki, self.action, self.ctx = aki, action, ctx
async def callback(self, interaction: discord.Interaction):
if interaction.user != self.ctx.author:
return await interaction.response.send_message(content=f"<@{self.ctx.author.id}> is playing this game! Use `{await get_guild_prefix(self.ctx)}aki` to create your own game.",
ephemeral=True)
if self.action == 'y':
embed_win = discord.Embed(title='GG!', color=0x00FF00)
embed_win.add_field(name=self.aki.name_proposition, value=self.aki.description_proposition, inline=False)
embed_win.set_image(url=self.aki.photo)
# embed_win.add_field(name="Ranking", value="#"+self.aki.first_guess['ranking'], inline=True)
embed_win.add_field(name="Questions", value=self.aki.step+1, inline=True)
embed_win.add_field(name="Progress", value=f"{self.aki.progression}%", inline=True)
if self.ctx.author.avatar: embed_win.set_author(name=self.ctx.author, icon_url=self.ctx.author.avatar.url)
else: embed_win.set_author(name=self.ctx.author)
await interaction.response.edit_message(embed=embed_win, view=None)
else:
# FIXME: problematic, might remove
# if self.aki.step < 79:
# try: await self.aki.exclude()
# except: pass
# return await interaction.response.edit_message(embed=qEmbed(self.aki, self.ctx), view=QView(self.aki, self.ctx))
embed_loss = discord.Embed(title="Game over!", description="Please try again.", color=0xFF0000) # Here's some of my guesses:
# for times in self.aki.guesses:
# embed_loss.add_field(name=times['name'], value=times['description'])
if self.ctx.author.avatar: embed_loss.set_author(name=self.ctx.author, icon_url=self.ctx.author.avatar.url)
else: embed_loss.set_author(name=self.ctx.author)
await interaction.response.edit_message(embed=embed_loss, view=None)
# @commands.max_concurrency(1, per=BucketType.default, wait=False)
async def Aki(ctx: commands.Context, cat: str, lang: str):
if await command_check(ctx, "aki", "games"): return await ctx.reply("command disabled", ephemeral=True)
msg = await ctx.reply('Starting game…')
if not cat: cat = 'people'
if not lang: lang = 'en'
sfw = not ctx.channel.nsfw if ctx.guild else True
if not lang in languages:
return await msg.edit(content=f"Invalid language parameter.\nSupported languages:```{languages}```")
if not cat in categories:
return await msg.edit(content=f'Category `{cat}` not found.\nAvailable categories:```{categories}```')
try:
aki = Akinator()
await aki.start_game(language=f'{lang}' if cat == 'people' else f'{lang}_{cat}', child_mode=sfw)
await msg.edit(content=None, embed=qEmbed(aki, ctx), view=QView(aki, ctx))
except Exception as e: await msg.edit(content=f"Error! :(\n{e}")
async def cat_auto(interaction: discord.Interaction, current: str) -> list[app_commands.Choice[str]]:
return [
app_commands.Choice(name=cat, value=cat) for cat in categories if current.lower() in cat.lower()
]
async def lang_auto(interaction: discord.Interaction, current: str) -> list[app_commands.Choice[str]]:
return [
app_commands.Choice(name=lang, value=lang) for lang in languages if current.lower() in lang.lower()
]
class CogAki(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.hybrid_command(description=f'{description_helper["emojis"]["games"]} {description_helper["games"]["aki"]}')
@app_commands.autocomplete(category=cat_auto, language=lang_auto)
@app_commands.describe(category="Set category", language="Set language")
@app_commands.allowed_installs(guilds=True, users=True)
@app_commands.allowed_contexts(guilds=True, dms=True, private_channels=True)
# @commands.max_concurrency(1, per=BucketType.default, wait=False)
async def aki(self, ctx: commands.Context, category:str=None, language:str=None):
await Aki(ctx, category, language)
async def setup(bot: commands.Bot):
await bot.add_cog(CogAki(bot))