deny adding a command who already exist in the bot itself

This commit is contained in:
Mylloon 2021-07-30 00:17:35 +02:00
parent 8058ab1453
commit c50cc204d9
3 changed files with 36 additions and 15 deletions

View file

@ -1,5 +1,5 @@
from twitchio.ext import commands from twitchio.ext import commands
from utils.core import load from utils.core import load, listCommands
from utils.commands import CommandesDB from utils.commands import CommandesDB
def prepare(client: commands.Bot): 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: if commandName == None or commandMessage == None:
return return
if ctx.author.is_mod: if ctx.author.is_mod:
if CommandesDB().existeCommande(commandName)[0] == False: if CommandesDB().existeTouteCommande(self.client, commandName)[0] == False:
CommandesDB().ajoutCommande(commandName, commandMessage) CommandesDB().ajoutCommande(commandName, commandMessage)
await ctx.send(f"@{ctx.author.name}, commande {commandName} ajoutée !") await ctx.send(f"@{ctx.author.name}, commande {commandName} ajoutée !")
else: else:
@ -46,9 +46,7 @@ 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""" """Affiche la liste des commandes de la base de donnée du bot"""
commandes = CommandesDB().listeCommande() commandes = CommandesDB().listeCommande()
cogs = self.client.cogs.values() for command in listCommands(self.client):
for cog in cogs:
for command in cog.commands.values():
name = command.name name = command.name
if command.no_global_checks: if command.no_global_checks:
if not ctx.author.is_mod: if not ctx.author.is_mod:

View file

@ -1,4 +1,5 @@
from utils.db import Database from utils.db import Database
from utils.core import listCommands
class CommandesDB(Database): class CommandesDB(Database):
def __init__(self): def __init__(self):
@ -44,3 +45,17 @@ class CommandesDB(Database):
if commande[0] == command: if commande[0] == command:
return (True, commande[1]) return (True, commande[1])
return (False,) 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,)

View file

@ -17,3 +17,11 @@ def load(variables):
exit(1) exit(1)
return keys 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