From 89077db935d29c010d24454274099172bdc704ba Mon Sep 17 00:00:00 2001 From: Shadow15510 Date: Mon, 12 Jun 2023 23:28:52 +0200 Subject: [PATCH] Add command aliases and better help --- fun_cmnds.py | 28 +++++++++++++++++++--------- glados_cmnds.py | 4 ---- irc_api/api.py | 11 ++++++----- main.py | 6 ++++-- 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/fun_cmnds.py b/fun_cmnds.py index 13a4ef9..7d79b9b 100644 --- a/fun_cmnds.py +++ b/fun_cmnds.py @@ -1,14 +1,24 @@ from irc_api import api from irc_api.api import auto_help +from random import randint, choice + +@api.every(30, desc="Fait apparaître un canard de manière aléatoire") +def pop_duck(bot): + if randint(1, 10) == 1: + ducks = ("Coin coin ! 🦆", "Couac ! 🦆") + bot.nb_ducks += 1 + bot.send("#glados", choice(ducks)) + if randint(1, 10) == 1 and bot.nb_ducks > 1: + bot.send("#glados", "Le canard est parti…") + bot.nb_ducks -= 1 + @api.channel("#glados") -@api.on(lambda m: "blague" in m.text.lower()) -def fun(bot, msg): - bot.send(msg.to, "Mais je suis nuuuullle en blagues euh.") - - -@api.user("Shadow15510") -@api.on(lambda m: "glados" in m.text.lower()) -def agree(bot, msg): - bot.send(msg.to, "Ouaip ! Complètement d'accord avec toi !") \ No newline at end of file +@api.command("pan", desc="Tirer sur un canard") +def fire(bot, msg): + if bot.nb_ducks > 0: + bot.nb_ducks -= 1 + bot.send("#glados", "Touché !") + else: + bot.send("#glados", "Euh va check ta vue stp…") diff --git a/glados_cmnds.py b/glados_cmnds.py index 6eba275..3437b24 100644 --- a/glados_cmnds.py +++ b/glados_cmnds.py @@ -55,7 +55,3 @@ def wiki(bot, msg, text: str, limit: int=1): bot.send(msg.to, f"{len(response[1])} résultat{('', 's')[limit > 1]} pour la recherche : '{text}'.") for name, link in zip(response[1], response[3]): bot.send(msg.to, f" {name} : {link}") - -@api.every(5) -def foo(bot): - bot.send("#glados", "foo called") diff --git a/irc_api/api.py b/irc_api/api.py index 14bec20..d758bea 100644 --- a/irc_api/api.py +++ b/irc_api/api.py @@ -1,6 +1,5 @@ import logging import re -from functools import wraps from irc_api.irc import IRC, History from threading import Thread import time @@ -9,12 +8,14 @@ import time PREFIX = "" -def command(name, desc=""): +def command(name, alias=(), desc=""): + if not alias or not name in alias: + alias += (name,) def decorator(func): return Command( name=name, func=func, - events=[lambda m: m.text.startswith(PREFIX + name)], + events=[lambda m: True in [m.text.startswith(PREFIX + cmd) for cmd in alias]], desc=desc, cmnd_type=1 ) @@ -215,7 +216,7 @@ class Bot: else: self.callbacks[command.name] = command - if add_to_help: + if add_to_help and command.cmnd_type == 1: self.commands_help[command.name] = command def add_commands(self, *commands, **kwargs): @@ -243,7 +244,7 @@ class Bot: self.callbacks.pop(command_name) -@command("aide") +@command("aide", alias=("aide", "help", "doc", "assistance")) def auto_help(bot, msg, fct_name: str=""): """Aide des commandes disponibles.""" if fct_name and fct_name in bot.commands_help.keys(): diff --git a/main.py b/main.py index 297f496..0ed33af 100755 --- a/main.py +++ b/main.py @@ -21,14 +21,16 @@ from secrets import USER, PASSWORD LOG_FORMAT = "%(asctime)s [%(levelname)s] <%(filename)s> %(funcName)s: %(message)s" logging.basicConfig(format=LOG_FORMAT, level=logging.DEBUG) +class MyBot(api.Bot): + nb_ducks = 0 -glados = api.Bot( + +glados = MyBot( (USER, PASSWORD), ('irc.planet-casio.com', 6697), ["#glados"], gv4_cmnds, fcmnds, prefix="!", - limit=50 )