diff --git a/README.md b/README.md index 75f8c4a..380c815 100644 --- a/README.md +++ b/README.md @@ -37,11 +37,15 @@ Here is the list of available commands by default: python3 -m pip install -r requirements.txt ``` - Rename `.envexample` file in `.env`, then open the file and complete the fields - | Field | Explanation | - | -------------- | ------------------------------------------------------------------------------------------------------------ | - | `ACCESS_TOKEN` | [Access token of the bot account](https://twitchtokengenerator.com/) (be sure you selected `Bot Chat` Token) | - | `PREFIX` | Prefix used for your bot | - | `CHANNEL` | Twitch channel(s) where the bot will run (separate by comma) | + + | Field | Explanation | + | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------ | + | `ACCESS_TOKEN` | [Access token of the bot account](https://twitchtokengenerator.com/) (be sure you selected `Bot Chat` Token) | + | `PREFIX` | Prefix used for your bot | + | `CHANNEL` | Twitch channel(s) where the bot will run (separate by comma) | + | `RIOT_CHANNEL`\* | Replace `CHANNEL` by the channel name, example: `RIOT_PONCE`, value should be your Riot ID, prefixed by the region, example: `eu/User#123` | + + > \* Available regions are: `EU`, `AP`, `NA`, `KR` - Start bot ```bash diff --git a/requirements.txt b/requirements.txt index 6ade626..9e9fe05 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ twitchio==2.8.2 python_dotenv==1.0.1 +requests-cache==1.1.1 diff --git a/src/modules/misc.py b/src/modules/misc.py new file mode 100644 index 0000000..024ba80 --- /dev/null +++ b/src/modules/misc.py @@ -0,0 +1,46 @@ +from datetime import timedelta +from sys import stderr + +from requests import get as http_get +from requests_cache import install_cache as cacher +from twitchio.ext.commands import Bot, Cog, Context +from twitchio.ext.commands import command as new +from utils.core import load + + +def prepare(client: Bot) -> None: + """Add the module to the client""" + client.add_cog(Misc(client)) + + +class Misc(Cog): + # Les méthodes avec no_global_checks=Vrai nécessite des droits de modération + + def __init__(self, client: Bot): + self.client = client + self.prefix = load(["PREFIX"])["PREFIX"] + self.not_a_mod = "tu n'es pas modérateur" + + @new(name="rank", aliases=["rang"]) + async def _rank(self, ctx: Context) -> None: + """Affiche le rang du jeu actuellement joué""" + game = (await self.client.fetch_channels([(await ctx.channel.user()).id]))[ + 0 + ].game_name + + match game: + case "VALORANT": + try: + value = f"RIOT_{ctx.message.channel.name.upper()}" + riot_infos = load([value])[value] + except Exception as e: + print(e, "Silently pass.", file=stderr) + else: + region, user_id = riot_infos.split("/") + name, tag = user_id.split("#") + + cacher("cache", expire_after=timedelta(minutes=30)) + api_link = f"https://api.kyroskoh.xyz/valorant/v1/mmr/{region}/{name}/{tag}?show=combo&display=0" + rank = http_get(api_link).text[:-1] + + await ctx.send(rank) diff --git a/src/utils/core.py b/src/utils/core.py index 587884a..bbe0f4f 100644 --- a/src/utils/core.py +++ b/src/utils/core.py @@ -1,12 +1,17 @@ from os import environ -from sys import exit +from sys import exit, stderr from dotenv import load_dotenv from twitchio.ext.commands import Bot, Command def load(variables: list[str]) -> dict: - """Load env variables""" + """ + Load env variables + + If not found and asked for a Riot account, raise an exception + Else exit + """ keys = {} load_dotenv() for var in variables: @@ -17,8 +22,15 @@ def load(variables: list[str]) -> dict: res = list(set(res.split(",")) - {""}) keys[var] = res except KeyError: - print(f"Please set the environment variable {var} (.env file supported)") - exit(1) + + if var.startswith("RIOT_"): + raise Exception(f"Riot ID not set for the channel '{var}'.") + else: + print( + f"Please set the environment variable {var} (.env file supported)", + file=stderr, + ) + exit(1) return keys