From c50cc204d9f9d8e0f5360d51e503b796099af53e Mon Sep 17 00:00:00 2001 From: Mylloon Date: Fri, 30 Jul 2021 00:17:35 +0200 Subject: [PATCH] deny adding a command who already exist in the bot itself --- src/modules/commandes.py | 28 +++++++++++++--------------- src/utils/commands.py | 15 +++++++++++++++ src/utils/core.py | 8 ++++++++ 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/modules/commandes.py b/src/modules/commandes.py index c8f240e..67a610f 100644 --- a/src/modules/commandes.py +++ b/src/modules/commandes.py @@ -1,5 +1,5 @@ from twitchio.ext import commands -from utils.core import load +from utils.core import load, listCommands from utils.commands import CommandesDB def prepare(client: commands.Bot): @@ -19,7 +19,7 @@ class Commandes(commands.Cog): # Les méthodes qui ont no_global_checks de Vrai if commandName == None or commandMessage == None: return if ctx.author.is_mod: - if CommandesDB().existeCommande(commandName)[0] == False: + if CommandesDB().existeTouteCommande(self.client, commandName)[0] == False: CommandesDB().ajoutCommande(commandName, commandMessage) await ctx.send(f"@{ctx.author.name}, commande {commandName} ajoutée !") else: @@ -46,19 +46,17 @@ class Commandes(commands.Cog): # Les méthodes qui ont no_global_checks de Vrai """Affiche la liste des commandes de la base de donnée du bot""" commandes = CommandesDB().listeCommande() - cogs = self.client.cogs.values() - for cog in cogs: - for command in cog.commands.values(): - name = command.name - if command.no_global_checks: - if not ctx.author.is_mod: - continue - if command.aliases: - name += " (alias: " - for aliase in command.aliases: - name += f"{self.keys['PREFIX']}{aliase}, " - name = f"{name[:-2]})" - commandes.append((name,)) + for command in listCommands(self.client): + name = command.name + if command.no_global_checks: + if not ctx.author.is_mod: + continue + if command.aliases: + name += " (alias: " + for aliase in command.aliases: + name += f"{self.keys['PREFIX']}{aliase}, " + name = f"{name[:-2]})" + commandes.append((name,)) if len(commandes) > 0: message = f"@{ctx.author.name}, liste des commandes -> " diff --git a/src/utils/commands.py b/src/utils/commands.py index a31eea9..877df2e 100644 --- a/src/utils/commands.py +++ b/src/utils/commands.py @@ -1,4 +1,5 @@ from utils.db import Database +from utils.core import listCommands class CommandesDB(Database): def __init__(self): @@ -44,3 +45,17 @@ class CommandesDB(Database): if commande[0] == command: return (True, commande[1]) return (False,) + + def existeTouteCommande(self, client, commande: str): + commandes = [] + for command in self.listeCommande(): + commandes.append(command[0]) + for command in listCommands(client): + commandes.append(command.name) + if command.aliases: + for comm in command.aliases: + commandes.append(comm) + if commande in commandes: + return (True,) + return (False,) + diff --git a/src/utils/core.py b/src/utils/core.py index 5c50c7d..4c81a7b 100644 --- a/src/utils/core.py +++ b/src/utils/core.py @@ -17,3 +17,11 @@ def load(variables): exit(1) return keys + +def listCommands(client): + cogs = client.cogs.values() + commands = [] + for cog in cogs: + for command in cog.commands.values(): + commands.append(command) + return commands