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