WIP on proposal

This commit is contained in:
Darks 2023-06-11 16:07:04 +02:00
parent 08507b3df0
commit cb6a43f843
Signed by: Darks
GPG Key ID: 7515644268BE1433
3 changed files with 89 additions and 0 deletions

11
proposal/README.md Normal file
View File

@ -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

59
proposal/commands.py Normal file
View File

@ -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!")

19
proposal/main.py Normal file
View File

@ -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 *