From ce5607fce3e08864d1f1f50a1dd6e245ba5adaad Mon Sep 17 00:00:00 2001 From: jackra1n <45038833+jackra1n@users.noreply.github.com> Date: Wed, 21 Feb 2024 13:44:55 +0100 Subject: [PATCH] Cleanup, send button with embed --- bot/cogs/karma.py | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/bot/cogs/karma.py b/bot/cogs/karma.py index 4acc30d..8a78f16 100644 --- a/bot/cogs/karma.py +++ b/bot/cogs/karma.py @@ -558,9 +558,12 @@ async def kasino_open(self, ctx: commands.Context, question: str, op_a: str, op_ if not ctx.interaction: await ctx.message.delete() async with ctx.typing(): - kasino_id = await self.add_kasino(ctx, question, op_a, op_b) + to_embed = discord.Embed(description="Opening kasino, hold on tight...") + kasino_msg = await ctx.send(embed=to_embed) + stmt_kasino = '''INSERT INTO kasino (discord_server_id, discord_channel_id, discord_message_id, question, option1, option2) + VALUES ($1, $2, $3, $4, $5, $6) RETURNING id''' + kasino_id = await self.bot.db.fetchval(stmt_kasino, ctx.guild.id, ctx.channel.id, kasino_msg.id, question, op_a, op_b) # TODO: Add kasino backup - # self.create_kasino_backup(kasino.id) await _update_kasino_msg(ctx, kasino_id) @kasino.command(name='close', usage="close ") @@ -825,14 +828,6 @@ async def send_conclusion(self, ctx: commands.Context, kasino_id: int, winner: i await ctx.send(embed=to_embed) return - async def add_kasino(self, ctx: commands.Context, question: str, option1: str, option2: str) -> int: - to_embed = discord.Embed(description="Opening kasino, hold on tight...") - kasino_msg = await ctx.send(embed=to_embed) - - stmt_kasino = '''INSERT INTO kasino (discord_server_id, discord_channel_id, discord_message_id, question, option1, option2) - VALUES ($1, $2, $3, $4, $5, $6) RETURNING id''' - return await self.bot.db.fetchval(stmt_kasino, ctx.guild.id, ctx.channel.id, kasino_msg.id, question, option1, option2) - async def remove_kasino(self, kasino_id: int) -> None: kasino = await self.bot.db.fetchrow('SELECT * FROM kasino WHERE id = $1', kasino_id) if kasino is None: @@ -845,13 +840,6 @@ async def remove_kasino(self, kasino_id: int) -> None: pass await self.bot.db.execute('DELETE FROM kasino WHERE id = $1', kasino_id) - def create_kasino_backup(self, kasino_id: int): - today_string = datetime.now().strftime("%Y_%m_%d") - now_time_string = datetime.now().strftime("%H%M") - backup_folder = f"{values.DATA_PATH}/backups/{today_string}" - Path(backup_folder).mkdir(parents=True, exist_ok=True) - shutil.copy(values.DB_PATH, f"{backup_folder}/backup_{now_time_string}_{kasino_id}.sqlite") - async def abort_kasino(self, kasino_id: int) -> None: stmt_kasino_and_bets = '''SELECT * FROM kasino JOIN kasino_bet ON kasino.id = kasino_bet.kasino_id WHERE kasino.id = $1''' @@ -916,10 +904,11 @@ async def win_kasino(self, kasino_id: int, winning_option: int): await user.send(embed=output) async def _update_kasino_msg(ctx: commands.Context, kasino_id: int) -> None: - kasino = await ctx.bot.db.fetchrow('SELECT * FROM kasino WHERE id = $1', kasino_id) + bot: Substiify = ctx.bot + kasino = await bot.db.fetchrow('SELECT * FROM kasino WHERE id = $1', kasino_id) k_channel_id = kasino['discord_channel_id'] k_message_id = kasino['discord_message_id'] - kasino_channel = await ctx.bot.fetch_channel(k_channel_id) + kasino_channel = await bot.fetch_channel(k_channel_id) kasino_msg = await kasino_channel.fetch_message(k_message_id) # FIGURE OUT AMOUNTS AND ODDS @@ -950,7 +939,7 @@ async def _update_kasino_msg(ctx: commands.Context, kasino_id: int) -> None: to_embed.add_field(name=f'**2:** {kasino["option2"]}', value=f'**Odds:** 1:{round(b_odds, 2)}\n**Pool:** {bets_b_amount} Karma') - await kasino_msg.edit(embed=to_embed) + await kasino_msg.edit(embed=to_embed, view=KasinoView(ctx, kasino)) class KasinoView(discord.ui.View): def __init__(self, ctx: commands.Context, kasino: Record): @@ -1042,7 +1031,7 @@ async def on_submit(self, interaction: discord.Interaction) -> None: await self.ctx.send(f"Bet added from {interaction.user}!", delete_after=30) -async def setup(bot): +async def setup(bot: Substiify): query = await bot.db.fetch('SELECT * FROM discord_channel WHERE upvote = True') upvote_channels = [channel['discord_channel_id'] for channel in query] or [] await bot.add_cog(Karma(bot, upvote_channels))