From cb6a43f843a31cb92f656ffb81c6a13b44008469 Mon Sep 17 00:00:00 2001 From: Darks Date: Sun, 11 Jun 2023 16:07:04 +0200 Subject: [PATCH] =?UTF-8?q?WIP=C2=A0on=20proposal?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- proposal/README.md | 11 +++++++++ proposal/commands.py | 59 ++++++++++++++++++++++++++++++++++++++++++++ proposal/main.py | 19 ++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 proposal/README.md create mode 100644 proposal/commands.py create mode 100644 proposal/main.py diff --git a/proposal/README.md b/proposal/README.md new file mode 100644 index 0000000..d2deecc --- /dev/null +++ b/proposal/README.md @@ -0,0 +1,11 @@ +This directory is a proposal of a library for making IRC bots on the v5. + +Some elements to have in mind: + +- This is a WIP + +## Overview of a Message object + +- `author`: user the message comes from +- `to`: channel the message is posted. Can be a user in case of MP +- `text`: the text of the message diff --git a/proposal/commands.py b/proposal/commands.py new file mode 100644 index 0000000..257a59a --- /dev/null +++ b/proposal/commands.py @@ -0,0 +1,59 @@ +# Import the relevant bot +from main import my_bot + + +# Register a free response to a message +# Here we reply to people mentionning the bot +# Note that this is a simple exemple that can bug if +# bot's name is included in a word (ex "Bobsleigh") +@my_bot.on(lambda m: my_bot.nick in m.text) +def hello(msg): + my_bot.send(msg.to, f"What's up, {m.author}") + +# Register a command on specific channels +@my_bot.on_channel("#fun", "#play") +@my_bot.command("!fun") +def cmd_fun(msg): + """ Give some fun, but only on authorized channels """ + my_bot.send(msg.to, "Fun is allowed only here") + +# Register a command on specific users +@my_bot.users("Breizh") +@my_bot.on(lambda m: True) +def breizh_is_always_right(msg): + """ Agree every message from Breizh """ + my_bot.send(msg.to, "I agree what Breizh said.") + +# Create commands using message history +@my_bot.on_channel("#duck_hunting") +@my_bot.command("!new") +def cmd_duck(msg): + my_bot.send(msg.to, "Hey, here's a duck!") + +@my_bot.on_channel("#duck_hunting") +@my_bot.on(lambda m: m.text.lower == "pan!") +def resp_duck(msg): + # Retreive index of last duck sent + index = None + for i, m in enumerate(reversed(my_bot.history)): + if m.author == my_bot.nick and m.to == "#duck_hunting" \ + and m.text = "Hey, here's a duck!": + index = i + break + + # If no duck found :( + if index is None: + my_bot.send(msg.to, "Where did you see a duck, idiot?") + return + + # Check if there was replies from other users since the last duck + for m in my_bot.history[-i:-2]: + if "pan!" in m.lower(): + my_bot.send(msg.to, f"{m.author} was quickier than you, looser!") + return + + # User was the quickiest, congrats! + my_bot.send(msg.to, f"Congrats {msg.author}, you are the winner!") + + + diff --git a/proposal/main.py b/proposal/main.py new file mode 100644 index 0000000..268968f --- /dev/null +++ b/proposal/main.py @@ -0,0 +1,19 @@ +# Import the library +from ircapi import Bot + + +# Create a new bot +my_bot = Bot( + credentials=("Bob", "MySuperPassword") # Use any method you want to import secrets + server=('irc.planet-casio.com', 6697) # Define the IRC server + channels=["#general", "#fun"], # Auto-join those channels + history=100 # Keep last 100 messages received +) + +# Register a simple command on the main file +@my_bot.command("!ping"): +def cmd_pong(msg): + my_bot.send(msg.to, "Pong!") + +# Or import them from another file +from commands import *