diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..1a424d5 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,10 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 4 +indent_style = space +insert_final_newline = false +max_line_length = 120 +tab_width = 4 diff --git a/bot/cogs/osu.py b/bot/cogs/osu.py index 0992260..049a090 100644 --- a/bot/cogs/osu.py +++ b/bot/cogs/osu.py @@ -9,10 +9,10 @@ from discord import Embed from discord.ext import commands from bot.bot import config -from helpers.converters import ArgumentConverter from utils.api import * from utils.db import users from utils.enums import GameModes, Grades, Mods, filter_invalid_combos +from helpers.converters import ArgumentConverter domain = config.get("domain") @@ -45,57 +45,15 @@ def construct_avatar_url(player_id): return url + f"?{str(int(time.time()))}" -def parsecommand(input_str): - """ - Returns a tuple containing the username and GameMode - :return: tuple[str, GameModes] - """ - gamemodes = { - '-std': GameModes.STANDARD, - '-rx': GameModes.RX_STANDARD, - '-taiko': GameModes.TAIKO, - '-taikorx': GameModes.RX_TAIKO, - '-ctb': GameModes.CATCH, - '-ctbrx': GameModes.RX_CATCH, - '-mania': GameModes.MANIA, - '-ap': GameModes.AP_STANDARD, - } - - mode = None - username = '' - - if not len(input_str) > 0: - mode = GameModes.STANDARD - return username, mode - - tokens = input_str.split() - for i, token in enumerate(tokens): - if token.startswith('-'): - mode = token - username = ' '.join(tokens[:i]) - break - - if mode is None: - mode = GameModes.STANDARD - username = ' '.join(tokens) - return username, mode - - try: - mode = gamemodes[mode] - except KeyError: - mode = None - - return username, mode - - async def get_username_and_mode( - ctx, username: str = None, mode: GameModes = GameModes.STANDARD + ctx, username: str = None, mode: GameModes = GameModes.STANDARD ): """ Returns a tuple containing the username and GameMode :return: tuple[str, GameModes] :raises ValueError: Set your username using **!setuser**. """ + if username: try: _mode = await ArgumentConverter().convert(ctx, username) @@ -110,7 +68,7 @@ async def get_username_and_mode( if not user and not username: raise ValueError("Set your username using **!setuser**.") else: - return user, mode + return user, mode or GameModes.STANDARD class Cog(commands.Cog, name="osu!"): @@ -129,17 +87,8 @@ async def setuser(self, ctx, *, username): ) @commands.command() - async def rs(self, ctx, *, parameters=''): - user, mode = parsecommand(parameters) - - if not mode: - return await ctx.send("Invalid game mode, please use any of the follow arguments: " - "-std | -rx | -ap | -taiko | -taikorx | -ctb | -ctbrx | -mania") - if not user: - users.get(ctx.author.id) - if not user: - return await ctx.send("Set your username using **!setuser**.") - + async def rs(self, ctx, username: str = None, mode: ArgumentConverter = GameModes.STANDARD): + user, mode = await get_username_and_mode(ctx, username, mode) data = await api.get( "get_player_scores", {"name": user, "scope": "recent", "limit": 1, "mode": mode.value}, @@ -286,6 +235,7 @@ def get_level(score): url=f'https://osu.{domain}/u/{player["id"]}', icon_url=f'https://osu.{domain}/static/images/flags/{player["country"].upper()}.png', ) + embed.set_thumbnail(url=construct_avatar_url(player["id"])) return await ctx.send(embed=embed) diff --git a/helpers/converters.py b/helpers/converters.py index 59f1f39..2501c46 100644 --- a/helpers/converters.py +++ b/helpers/converters.py @@ -62,5 +62,5 @@ async def convert(self, ctx, arg) -> GameModes: if len(arguments) == 1: if arg in game_modes: return game_modes[arg] - else: - raise ValueError("Invalid game mode and/or mod") + + raise ValueError("Invalid game mode and/or mod") \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 3a248c4..18e8d10 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,6 @@ requests discord~=2.3.2 pyyaml~=6.0 python-dotenv~=1.0.1 -humanize~=4.8.0 +humanize~=4.9.0 sqlitedict~=2.1.0 -aiohttp~=3.8.6 +aiohttp~=3.9.3 diff --git a/utils/api.py b/utils/api.py index f126685..c3d7f75 100644 --- a/utils/api.py +++ b/utils/api.py @@ -60,6 +60,8 @@ async def get(self, path: str, params: dict = None): if status := data.get("status"): raise ValueError(status) + elif msg := data.get("msg"): + raise ValueError(msg) else: logging.error( f"Generic error exception was raised for the following response: {data}"