Add rank command (#1)
All checks were successful
ci/woodpecker/push/publish Pipeline was successful

This commit is contained in:
Mylloon 2024-02-06 12:36:19 +01:00
parent f979b8c4c1
commit e33c9ac930
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 72 additions and 9 deletions

View file

@ -37,11 +37,15 @@ Here is the list of available commands by default:
python3 -m pip install -r requirements.txt python3 -m pip install -r requirements.txt
``` ```
- Rename `.envexample` file in `.env`, then open the file and complete the fields - Rename `.envexample` file in `.env`, then open the file and complete the fields
| Field | Explanation | | Field | Explanation |
| -------------- | ------------------------------------------------------------------------------------------------------------ | | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
| `ACCESS_TOKEN` | [Access token of the bot account](https://twitchtokengenerator.com/) (be sure you selected `Bot Chat` Token) | | `ACCESS_TOKEN` | [Access token of the bot account](https://twitchtokengenerator.com/) (be sure you selected `Bot Chat` Token) |
| `PREFIX` | Prefix used for your bot | | `PREFIX` | Prefix used for your bot |
| `CHANNEL` | Twitch channel(s) where the bot will run (separate by comma) | | `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 - Start bot
```bash ```bash

View file

@ -1,2 +1,3 @@
twitchio==2.8.2 twitchio==2.8.2
python_dotenv==1.0.1 python_dotenv==1.0.1
requests-cache==1.1.1

46
src/modules/misc.py Normal file
View file

@ -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)

View file

@ -1,12 +1,17 @@
from os import environ from os import environ
from sys import exit from sys import exit, stderr
from dotenv import load_dotenv from dotenv import load_dotenv
from twitchio.ext.commands import Bot, Command from twitchio.ext.commands import Bot, Command
def load(variables: list[str]) -> dict: 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 = {} keys = {}
load_dotenv() load_dotenv()
for var in variables: for var in variables:
@ -17,7 +22,14 @@ def load(variables: list[str]) -> dict:
res = list(set(res.split(",")) - {""}) res = list(set(res.split(",")) - {""})
keys[var] = res keys[var] = res
except KeyError: except KeyError:
print(f"Please set the environment variable {var} (.env file supported)")
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) exit(1)
return keys return keys