From 20efbf7b3504f2011c4b6e88018da280c052a891 Mon Sep 17 00:00:00 2001 From: jackra1n <45038833+jackra1n@users.noreply.github.com> Date: Tue, 19 Mar 2024 13:53:08 +0100 Subject: [PATCH] Fix sending error message to channel, fix kasino odds, fix getting user in karma donate --- core/bot.py | 2 +- extensions/karma.py | 49 +++++++++++++++++++++++++++++---------------- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/core/bot.py b/core/bot.py index 4f88d96..454083e 100644 --- a/core/bot.py +++ b/core/bot.py @@ -87,7 +87,7 @@ async def on_command_error(self, ctx: commands.Context, error) -> None: ERRORS_CHANNEL_ID = 1219407043186659479 embed = discord.Embed(title="Error", description=f"```{error}```", color=discord.Color.red()) - await self.bot.get_channel(ERRORS_CHANNEL_ID).send(embed=embed) + await self.get_channel(ERRORS_CHANNEL_ID).send(embed=embed) try: await ctx.message.add_reaction("❌") diff --git a/extensions/karma.py b/extensions/karma.py index f109984..811725e 100644 --- a/extensions/karma.py +++ b/extensions/karma.py @@ -1,5 +1,6 @@ import logging import os +import re from datetime import datetime import discord @@ -171,17 +172,10 @@ async def karma_donate(self, ctx: commands.Context, *args): for arg in args: if user is None: - try: - user = await commands.MemberConverter().convert(ctx, arg) - continue - except commands.BadArgument: - pass - - if amount is None: - try: - amount = int(arg) - except ValueError: - continue + user = self._find_guild_user(ctx.guild, arg) + + if amount is None and arg.isdigit(): + amount = int(arg) logger.debug(f"Karma transfer params -> User: {user}, amount: {amount}") if user is None or amount is None: @@ -220,6 +214,27 @@ async def karma_donate(self, ctx: commands.Context, *args): embed.description = f"{ctx.author.mention} has donated {amount} karma to {user.mention}!" await ctx.send(embed=embed) + def _find_guild_user(self, guild: discord.Guild, arg: str) -> discord.Member | None: + members = guild.members + match = commands.IDConverter._get_id_match(arg) or re.match(r'<#([0-9]{15,20})>$', arg) + if match is None: + # not a mention or an id + username, _, discriminator = arg.rpartition('#') + + # If # isn't found then "discriminator" actually has the username + if not username: + discriminator, username = username, discriminator + + if discriminator == '0' or (len(discriminator) == 4 and discriminator.isdigit()): + return discord.utils.find(lambda m: m.name == username and m.discriminator == discriminator, members) + + def pred(m: discord.Member) -> bool: + return m.name == arg or m.global_name == arg + + return discord.utils.find(pred, members) + else: + return guild.get_member(int(arg)) + @karma_donate.error async def karma_donate_error(self, ctx: commands.Context, error): embed = discord.Embed(color=0xF66045) @@ -906,8 +921,8 @@ async def _update_kasino_msg(bot: core.Substiify, kasino_id: int) -> None: # FIGURE OUT AMOUNTS AND ODDS stmt_kasino_bets_sum = """SELECT SUM(amount) FROM kasino_bet WHERE kasino_id = $1 AND option = $2""" - bets_a_amount = await bot.db.fetchval(stmt_kasino_bets_sum, kasino_id, 1) or 0.0 - bets_b_amount = await bot.db.fetchval(stmt_kasino_bets_sum, kasino_id, 2) or 0.0 + bets_a_amount: int = await bot.db.fetchval(stmt_kasino_bets_sum, kasino_id, 1) or 0 + bets_b_amount: int = await bot.db.fetchval(stmt_kasino_bets_sum, kasino_id, 2) or 0 a_odds, b_odds = _calculate_odds(bets_a_amount, bets_b_amount) # CREATE MESSAGE @@ -937,10 +952,10 @@ async def _update_kasino_msg(bot: core.Substiify, kasino_id: int) -> None: await kasino_msg.edit(embed=embed, view=KasinoView(kasino)) -def _calculate_odds(bets_a_amount: float, bets_b_amount: float) -> tuple[float, float]: - total_bets: float = float(bets_a_amount) + float(bets_b_amount) - a_odds: float = float(total_bets) / float(bets_a_amount) if bets_a_amount else 1.0 - b_odds: float = float(total_bets) / float(bets_b_amount) if bets_b_amount else 1.0 +def _calculate_odds(bets_a_amount: int, bets_b_amount: int) -> tuple[float, float]: + total_bets: float = float(bets_a_amount + bets_b_amount) + a_odds: float = total_bets / float(bets_a_amount) if bets_a_amount else 1.0 + b_odds: float = total_bets / float(bets_b_amount) if bets_b_amount else 1.0 return a_odds, b_odds