GLaDOS/glados_cmnds.py

69 lines
2.6 KiB
Python

from random import choice
import re
import requests
import time
from irc_api import api
from irc_api.api import auto_help
@api.on(lambda m: isinstance(re.match(r"(.*)(bonjour|coucou|salut|hey|hello|hi|yo) glados(.*)", m.text, re.IGNORECASE), re.Match))
def greetings_hello(bot, msg):
"""Dit bonjour à l'utilisateur"""
greetings = ("Bonjour", "Coucou", "Salut", "Hey", "Hello", "Hi", "Yo")
bot.send(msg.to, f"{choice(greetings)} {msg.author}.")
@api.on(lambda m: isinstance(re.match(r"(.*)(au revoir|à plus|a\+|à toute|bye|see you|see you soon) glados(.*)", m.text, re.IGNORECASE), re.Match))
def greetings_bye(bot, msg):
"""Dit au revoir à l'utilisateur."""
greetings = ("Au revoir", "À plus", "À toute", "Bye bye", "See you soon")
bot.send(msg.to, f"{choice(greetings)} {msg.author}.")
@api.on(lambda m: isinstance(re.match(r"(.*)(merci|merci beaucoup|thx|thanks|thank you) glados(.*)", m.text, re.IGNORECASE), re.Match))
def thanks(bot, msg):
"""Répond aux remerciements de l'utilisateur."""
thanks_choice = ("Mais je vous en prie", "Tout le plaisir est pour moi", "C'est tout naturel", "Je n'en ferais rien")
bot.send(msg.to, f"{choice(thanks_choice)}, {msg.author}")
@api.on(lambda m: isinstance(re.match(r"(.*)quelle heure(.*)?", m.text, re.IGNORECASE), re.Match))
def hour(bot, msg):
"""Donne l'heure actuelle"""
now = time.strftime("%Hh%M", time.localtime())
bot.send(msg.to, f"Il est très exactement {now}.")
@api.command("wiki", desc="wiki <recherche> [limite=1]\nFait une recherche wikipedia.")
def wiki(bot, msg, text: str, limit: int=1):
session = requests.Session()
params = {
'action': 'opensearch',
'search': text,
'limit': limit,
'namespace': 0,
'redirects': 'resolve',
'format': 'json'
}
response = session.get(url="https://fr.wikipedia.org/w/api.php", params=params, timeout=(4, 12)).json()
if len(response[1]) == 0:
bot.send(msg.to, f"Aucun résultat trouvé pour la recherche : {text}.")
else:
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.channel("#glados")
def spam_on_glados(bot, msg):
"""Détecte les monologues sur #glados."""
if len(bot.history) > 2 and bot.history.get()[-2].author == msg.author:
bot.counter += 1
else:
bot.counter = 1
if bot.counter > 10:
bot.send(msg.to, f"Bon, {msg.author}, tu vas arrêter de spammer un jour ou bien ?")
bot.counter = 0