Skip to content

Commit

Permalink
Cleanup, send button with embed
Browse files Browse the repository at this point in the history
  • Loading branch information
jackra1n committed Feb 21, 2024
1 parent 8210a74 commit ce5607f
Showing 1 changed file with 10 additions and 21 deletions.
31 changes: 10 additions & 21 deletions bot/cogs/karma.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <kasino_id> <winning_option>")
Expand Down Expand Up @@ -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:
Expand All @@ -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'''
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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))

0 comments on commit ce5607f

Please sign in to comment.