From f221bd2081e70cde3acd38a03ad87b9f82ec7f58 Mon Sep 17 00:00:00 2001 From: Iqrar Agalosi Nureyza Date: Fri, 26 Jan 2024 18:46:32 +0700 Subject: [PATCH] Refactor tatsu wrapper --- bot/module/tatsu/wrapper.py | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/bot/module/tatsu/wrapper.py b/bot/module/tatsu/wrapper.py index a67b694..7e7bfc4 100644 --- a/bot/module/tatsu/wrapper.py +++ b/bot/module/tatsu/wrapper.py @@ -111,9 +111,11 @@ async def get_guild_rankings(self, guild_id, timeframe="all", offset=0) -> ds.Gu ) return rankings - async def add_score(self, guild_id: int, user_id: int, amount: int) -> ds.GuildScoreObject: + async def _modify_score( + self, action_type: int, guild_id: int, user_id: int, amount: int + ) -> ds.GuildScoreObject: url = f"/guilds/{guild_id}/members/{user_id}/score" - payload = {'action': 0, 'amount': amount} + payload = {'action': action_type, 'amount': amount} try: result = await self.patch(url, payload) @@ -127,18 +129,8 @@ async def add_score(self, guild_id: int, user_id: int, amount: int) -> ds.GuildS ) return score - async def subtract_score(self, guild_id: int, user_id: int, amount: int) -> ds.GuildScoreObject: - url = f"/guilds/{guild_id}/members/{user_id}/score" - payload = {'action': 1, 'amount': amount} - - try: - result = await self.patch(url, payload) - except Exception as e: - raise e + async def add_score(self, guild_id: int, user_id: int, amount: int) -> ds.GuildScoreObject: + return await self._modify_score(0, guild_id, user_id, amount) - score = ds.GuildScoreObject( - guild_id=result.get('guild_id'), - score=result.get('score'), - user_id=result.get('user_id'), - ) - return score + async def remove_score(self, guild_id: int, user_id: int, amount: int) -> ds.GuildScoreObject: + return await self._modify_score(1, guild_id, user_id, amount)