change gitlab changelogs + new loading of env variables
This commit is contained in:
parent
49c3f53f42
commit
66c2e2c241
1 changed files with 34 additions and 15 deletions
|
@ -1,7 +1,9 @@
|
|||
import re
|
||||
import json
|
||||
import requests
|
||||
from re import findall
|
||||
from time import time
|
||||
from os import environ, path
|
||||
from dotenv import load_dotenv
|
||||
|
||||
def map_list_among_us(map):
|
||||
"""Sélecteur de map pour la commande amongus"""
|
||||
|
@ -53,7 +55,7 @@ def cleanCodeStringWithMentionAndURLs(string):
|
|||
def getMentionInString(string):
|
||||
"""récupère les mentions dans un string"""
|
||||
findedMention = []
|
||||
for findingMention in re.findall(r'<@[!]?\d*>', string): # récupération mention dans le string
|
||||
for findingMention in findall(r'<@[!]?\d*>', string): # récupération mention dans le string
|
||||
findedMention.append(findingMention)
|
||||
findedMention = list(dict.fromkeys(findedMention)) # suppression doublon de mention dans la liste
|
||||
return findedMention
|
||||
|
@ -61,7 +63,7 @@ def getMentionInString(string):
|
|||
def getURLsInString(string):
|
||||
"""récupère les liens dans un string"""
|
||||
findedURLs = []
|
||||
for findingMention in re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', string): # récupération URLs dans le string
|
||||
for findingMention in findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', string): # récupération URLs dans le string
|
||||
findedURLs.append(findingMention)
|
||||
return findedURLs
|
||||
|
||||
|
@ -114,19 +116,23 @@ def mentionToUser(mention: str):
|
|||
"""Récupère une mention et renvois son ID"""
|
||||
return int(mention[2:-1].replace("!",""))
|
||||
|
||||
def getChangelogs(version = 'latest'):
|
||||
def getChangelogs(version = 'actual'):
|
||||
"""Récupère les changements d'une version (par défaut, la dernière en date) et renvois dans l'ordre : url, n° version, changements"""
|
||||
if version != 'latest':
|
||||
version = f'tags/v{version}'
|
||||
changements = requests.get(f"https://api.github.com/repos/Confrerie-du-Kassoulait/KassouBot/releases/{version}").json()
|
||||
try:
|
||||
if changements["message"].startswith("API"): # renvois 0 si c'est une erreur API
|
||||
return 0
|
||||
else: # renvois None si aucune version correspondante n'a été trouvée
|
||||
return None
|
||||
except:
|
||||
pass
|
||||
return [changements["html_url"], changements["tag_name"][1:], changements["body"]]
|
||||
if version == 'actual':
|
||||
version = getActualVersion()
|
||||
changements = requests.get(f"https://gitlab.com/api/v4/projects/28424435/releases/v{version}")
|
||||
|
||||
if changements.status_code != 200: # si pas valide
|
||||
return [changements.status_code]
|
||||
else:
|
||||
code = 200
|
||||
|
||||
changements = changements.json()
|
||||
return [code, changements["_links"]["self"], changements["tag_name"][1:], changements["description"]]
|
||||
|
||||
def getActualVersion():
|
||||
with open(path.join(path.dirname(path.dirname(path.dirname(__file__))), "README.md"), "r") as file:
|
||||
return findall(r'https://img.shields.io/badge/version-(\d+\.\d+)', file.readlines()[2])[0]
|
||||
|
||||
def isSlash(arg):
|
||||
"""Regarde si la commande viens d'un slash ou pas, retourne l'argument sans le 'True' si c'est le cas"""
|
||||
|
@ -154,3 +160,16 @@ async def mySendHidden(
|
|||
await ctx.send( # sending normal message
|
||||
content = message, tts = tts, embed = embed, file = file, files = files,
|
||||
delete_after = delete_after, allowed_mentions = allowed_mentions)
|
||||
|
||||
def load(variables):
|
||||
"""Load env variables"""
|
||||
keys = {}
|
||||
load_dotenv() # load .env file
|
||||
for var in variables:
|
||||
try:
|
||||
keys[var] = environ[var]
|
||||
except KeyError:
|
||||
print(f"Please set the environment variable {var} (.env file supported)")
|
||||
exit(1)
|
||||
|
||||
return keys
|
||||
|
|
Reference in a new issue