minor refactor
All checks were successful
ci/woodpecker/push/publish Pipeline was successful

This commit is contained in:
Mylloon 2024-01-24 19:29:28 +01:00
parent 1962af8c7e
commit 589e062ad2
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 70 additions and 36 deletions

View file

@ -1,41 +1,61 @@
from twitchio.ext import commands from typing import cast
from twitchio import Message
from twitchio.chatter import Chatter
from twitchio.ext.commands import Bot, Cog, Context
from twitchio.ext.commands import command as new
from utils.commands import CommandesDB, existeCommande, existeTouteCommande from utils.commands import CommandesDB, existeCommande, existeTouteCommande
from utils.core import listCommands, load from utils.core import listCommands, load
def prepare(client: commands.Bot): def prepare(client: Bot):
client.add_cog(Commandes(client)) client.add_cog(Commandes(client))
class Commandes(commands.Cog): class Commandes(Cog):
# Les méthodes qui ont no_global_checks = Vrai ne sont autorisés qu'aux modérateurs""" # Les méthodes avec no_global_checks=Vrai nécessite des droits de modération
def __init__(self, client: commands.Bot):
self.client = client
self.keys = load(["PREFIX"])
self.notModo = "tu n'es pas modérateur"
self.notExistingCommand = "cette commande n'existe pas ou est intégré au bot"
self.alreadyExistingCommand = "cette commande existe déjà"
@commands.command(name="add", no_global_checks=True) def __init__(self, client: Bot):
async def _add(self, ctx: commands.Context, commandName=None, commandMessage=None): self.client = client
"""Ajoute une commande de la base de donnée du bot : add nomDeLaCommande messageDeLaCommande""" self.prefix = load(["PREFIX"])["PREFIX"]
if commandName is None or commandMessage is None: self.notModo = "tu n'es pas modérateur"
self.notExistingCommand = "cette commande n'existe pas"
self.alreadyExistingCommand = (
f"cette commande existe déjà, {self.prefix}edit <name> <msg> pour l'éditer"
)
@new(name="add", aliases=["ajout", "ajouter"], no_global_checks=True)
async def _add(
self, ctx: Context, name: str | None = None, message: str | None = None
) -> None:
"""
Ajoute une commande de la base de donnée du bot
`add name message`
"""
if name is None or message is None:
return return
if ctx.author.is_mod: # type: ignore author = cast(Chatter, ctx.author)
if existeTouteCommande(self.client, commandName)[0] is False: if author.is_mod:
CommandesDB().ajoutCommande(commandName, commandMessage) if existeTouteCommande(self.client, name)[0] is False:
await ctx.send(f"@{ctx.author.name}, commande {commandName} ajoutée !") CommandesDB().ajoutCommande(name, message)
await ctx.send(f"@{ctx.author.name}, commande {name} ajoutée !")
else: else:
await ctx.send(f"@{ctx.author.name}, {self.alreadyExistingCommand}.") await ctx.send(f"@{ctx.author.name}, {self.alreadyExistingCommand}.")
else: else:
await ctx.send(f"@{ctx.author.name}, {self.notModo}.") await ctx.send(f"@{ctx.author.name}, {self.notModo}.")
@commands.command(name="remove", aliases=["delete"], no_global_checks=True) @new(name="remove", aliases=["delete", "suppr", "supprimer"], no_global_checks=True)
async def _remove(self, ctx: commands.Context, commandName=None): async def _remove(self, ctx: Context, commandName: str | None = None) -> None:
"""Supprime une commande de la base de donnée du bot : remove nomDeLaCommande""" """
Supprime une commande de la base de donnée du bot
`remove name`
"""
if commandName is None: if commandName is None:
return return
if ctx.author.is_mod: # type: ignore author = cast(Chatter, ctx.author)
if author.is_mod:
if existeCommande(commandName)[0]: if existeCommande(commandName)[0]:
CommandesDB().suppressionCommande(commandName) CommandesDB().suppressionCommande(commandName)
await ctx.send( await ctx.send(
@ -46,39 +66,50 @@ class Commandes(commands.Cog):
else: else:
await ctx.send(f"@{ctx.author.name}, {self.notModo}.") await ctx.send(f"@{ctx.author.name}, {self.notModo}.")
@commands.command(name="list", aliases=["commande", "commandes", "help"]) @new(name="list", aliases=["liste", "commands", "commandes", "help", "aide"])
async def _list(self, ctx: commands.Context): async def _list(self, ctx: Context) -> None:
"""Affiche la liste des commandes de la base de donnée du bot""" """Affiche la liste des commandes de la base de donnée du bot"""
commandes = CommandesDB().listeCommande() commandes = CommandesDB().listeCommande()
author = cast(Chatter, ctx.author)
for command in listCommands(self.client): for command in listCommands(self.client):
name = command.name name = command.name
if command.no_global_checks: if command.no_global_checks:
if not ctx.author.is_mod: # type: ignore if author.is_mod:
continue continue
if command.aliases: if command.aliases:
name += " (alias: " name += " (alias: "
for aliase in command.aliases: for aliase in command.aliases:
name += f"{self.keys['PREFIX']}{aliase}, " name += f"{self.prefix}{aliase}, "
name = f"{name[:-2]})" name = f"{name[:-2]})"
commandes.append((name,)) commandes.append((name,))
if len(commandes) > 0: if len(commandes) > 0:
message = f"@{ctx.author.name}, liste des commandes -> " message = f"@{ctx.author.name}, liste des commandes -> "
for commande in commandes: for commande in commandes:
message += f"{self.keys['PREFIX']}{commande[0]}, " message += f"{self.prefix}{commande[0]}, "
await ctx.send(message[:-2]) await ctx.send(message[:-2])
else: else:
await ctx.send( await ctx.send(
f"@{ctx.author.name}, aucune commande enrengistrée dans la base de donnée." f"@{ctx.author.name}, aucune commande enrengistrée dans la base de donnée."
) )
@commands.command(name="edit", no_global_checks=True) @new(name="edit", no_global_checks=True)
async def _edit(self, ctx: commands.Context, commandName=None, commandMessage=None): async def _edit(
"""Modifie une commande de la base de donnée du bot : add nomDeLaCommande nouveauMessageDeLaCommande""" self,
ctx: Context,
commandName: str | None = None,
commandMessage: str | None = None,
) -> None:
"""
Modifie une commande de la base de donnée du bot
add name new_message
"""
if commandName is None or commandMessage is None: if commandName is None or commandMessage is None:
return return
if ctx.author.is_mod: # type: ignore author = cast(Chatter, ctx.author)
if author.is_mod:
if existeCommande(commandName)[0]: if existeCommande(commandName)[0]:
CommandesDB().suppressionCommande(commandName) CommandesDB().suppressionCommande(commandName)
CommandesDB().ajoutCommande(commandName, commandMessage) CommandesDB().ajoutCommande(commandName, commandMessage)
@ -88,12 +119,15 @@ class Commandes(commands.Cog):
else: else:
await ctx.send(f"@{ctx.author.name}, {self.notModo}.") await ctx.send(f"@{ctx.author.name}, {self.notModo}.")
@commands.Cog.event() # type: ignore @Cog.event("event_message") # type: ignore
async def event_message(self, message): async def _event_message(self, message: Message) -> None:
if message.content.startswith(self.keys["PREFIX"]): if not message.content:
return
if message.content.startswith(self.prefix):
command = existeCommande( command = existeCommande(
message.content[1:].split(" ")[0] message.content[1:].split(" ")[0]
) # récupère le nom de la commande ) # récupère le nom de la commande
if command[0]: # vérification si existe if command[0]: # vérification si existe
# envois le contenu de la commande # envois le contenu de la commande
await message.channel.send(f"@{message.author.name}, {command[1]}") # type: ignore await message.channel.send(f"@{message.author.name}, {command[1]}")

View file

@ -47,7 +47,7 @@ def existeCommande(command: str):
for commande in commandes: for commande in commandes:
if commande[0] == command: if commande[0] == command:
return (True, commande[1]) return (True, commande[1])
return (False,) return (False, None)
def existeTouteCommande(client, commande: str): def existeTouteCommande(client, commande: str):