Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/pip/python-dotenv-approx-eq-1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
fuyu authored Apr 20, 2024
2 parents a9f9ddf + d8e4e7d commit 21bde21
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 61 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
64 changes: 7 additions & 57 deletions bot/cogs/osu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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)
Expand All @@ -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!"):
Expand All @@ -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},
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions helpers/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions utils/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down

0 comments on commit 21bde21

Please sign in to comment.