From f13c538daa0502ad2db518b65ec29bad4ab572b1 Mon Sep 17 00:00:00 2001 From: extreme4all <40169115+extreme4all@users.noreply.github.com> Date: Sat, 10 Aug 2024 11:47:35 +0200 Subject: [PATCH] normalize name --- src/cogs/rsn_linking_commands.py | 2 +- src/utils/bot_detector_api.py | 21 +++++++++++++-------- src/utils/string_processing.py | 3 +++ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/cogs/rsn_linking_commands.py b/src/cogs/rsn_linking_commands.py index 15b34d5..c761227 100644 --- a/src/cogs/rsn_linking_commands.py +++ b/src/cogs/rsn_linking_commands.py @@ -179,7 +179,7 @@ async def link(self, ctx: Context, *, name: str): # register verification await config.api.post_discord_code( - discord_id=ctx.author.id, player_name=player.get("name"), code=code + discord_id=ctx.author.id, name=player.get("name"), code=code ) # send user via pm the random code diff --git a/src/utils/bot_detector_api.py b/src/utils/bot_detector_api.py index 6885f0c..84a4154 100644 --- a/src/utils/bot_detector_api.py +++ b/src/utils/bot_detector_api.py @@ -4,6 +4,7 @@ import aiohttp from src import config +from src.utils.string_processing import to_jagex_name logger = logging.getLogger(__name__) @@ -198,6 +199,7 @@ async def create_player(self, name: str, debug: bool = False) -> None: None """ url = self.url + "/v1/player" + name = to_jagex_name(name=name) data = await self._webrequest( url, type="post", @@ -223,7 +225,7 @@ async def get_player(self, name: str, debug: bool = False) -> dict: """ url = self.url + "/v1/player" params = { - "player_name": name, + "player_name": to_jagex_name(name=name), "token": self.token, "row_count": 1, "page": 1, @@ -234,7 +236,7 @@ async def get_player(self, name: str, debug: bool = False) -> dict: return data # TODO: API design - async def get_discord_player(self, runescape_name: str) -> List[dict]: + async def get_discord_player(self, name: str) -> List[dict]: """ Get a player's Discord account by their RuneScape name. @@ -243,22 +245,23 @@ async def get_discord_player(self, runescape_name: str) -> List[dict]: player's Discord account information. Args: - runescape_name (str): The RuneScape name of the player to get. + name (str): The RuneScape name of the player to get. Returns: List[dict]: A list of dictionaries containing the player's Discord account information, or an empty list if the player was not found. """ + name = to_jagex_name(name=name) url = ( self.url - + f"/discord/verify/player_rsn_discord_account_status/{self.token}/{runescape_name}" + + f"/discord/verify/player_rsn_discord_account_status/{self.token}/{name}" ) data = await self._webrequest(url, type="get") return data # TODO: API design async def post_discord_code( - self, discord_id: str, player_name: int, code: str + self, discord_id: str, name: int, code: str ) -> List[dict]: """ Post a Discord verification code for a player. @@ -274,8 +277,9 @@ async def post_discord_code( Returns: None """ + name = to_jagex_name(name=name) url = self.url + f"/discord/verify/insert_player_dpc/{self.token}" - data = {"discord_id": discord_id, "player_name": player_name, "code": code} + data = {"discord_id": discord_id, "player_name": name, "code": code} await self._webrequest(url, type="post", json=data) # TODO: API design @@ -374,6 +378,7 @@ async def get_prediction(self, player_name: str, breakdown: bool = True) -> dict player was not found. """ url = self.url + "/v1/prediction" + player_name = to_jagex_name(name=player_name) params = {"name": player_name, "breakdown": int(breakdown)} data = await self._webrequest(url, type="get", params=params) return data @@ -429,7 +434,7 @@ async def get_latest_sighting(self, name: str): if the player was not found. """ url = self.url + f"/discord/get_latest_sighting/{self.token}" - params = {"player_name": name} + params = {"player_name": to_jagex_name(name=name)} data = await self._webrequest(url, type="post", json=params) return data @@ -448,6 +453,6 @@ async def get_xp_gainz(self, name: str): if the player was not found. """ url = self.url + f"/discord/get_xp_gains/{self.token}" - params = {"player_name": name} + params = {"player_name": to_jagex_name(name=name)} data = await self._webrequest(url, type="post", json=params) return data diff --git a/src/utils/string_processing.py b/src/utils/string_processing.py index 4b7b72f..5734e2b 100644 --- a/src/utils/string_processing.py +++ b/src/utils/string_processing.py @@ -20,3 +20,6 @@ def plus_minus(var, compare): diff_control = "+" return diff_control + +def to_jagex_name(name: str) -> str: + return name.lower().replace("_", " ").replace("-", " ").strip() \ No newline at end of file