rename 'commands' into 'api'
This commit is contained in:
parent
fe803f3bc4
commit
b0d6fbac85
|
@ -1,20 +1,20 @@
|
|||
from irc_api import commands
|
||||
from irc_api import api
|
||||
|
||||
|
||||
class GladosV4(commands.CommandsPack):
|
||||
@commands.command(name="bye", event=lambda m: "au revoir glados" in m.text.lower())
|
||||
class GladosV4(api.CommandsPack):
|
||||
@api.command(name="bye", event=lambda m: "au revoir glados" in m.text.lower())
|
||||
def greetings_bye(self, msg):
|
||||
"""Répond "au revoir"."""
|
||||
self.bot.irc.send(msg.to, f"Au revoir {msg.author}")
|
||||
|
||||
@commands.command(name="hello", event=lambda m: "bonjour glados" in m.text.lower())
|
||||
@api.command(name="hello", event=lambda m: "bonjour glados" in m.text.lower())
|
||||
def greetings_hello(self, msg):
|
||||
"""Dit bonjour lorsque l'on salue le GLaDOS."""
|
||||
self.bot.irc.send(msg.to, f"Bonjour {msg.author}")
|
||||
|
||||
|
||||
class GladosV5(commands.CommandsPack):
|
||||
@commands.command(name="test")
|
||||
class GladosV5(api.CommandsPack):
|
||||
@api.command(name="test")
|
||||
def test(self, msg):
|
||||
"""Test de module de fonction."""
|
||||
self.bot.irc.send(msg.to, "Ceci est un test")
|
|
@ -2,7 +2,6 @@ from functools import wraps
|
|||
import logging
|
||||
from secrets import USER, PASSWORD
|
||||
from irc_api.irc import IRC
|
||||
from irc_api.v5 import V5
|
||||
|
||||
|
||||
PREFIX = ""
|
||||
|
@ -60,15 +59,13 @@ class Bot:
|
|||
start : NoneType, public
|
||||
Runs the bot and connects it to IRC and V5 servers.
|
||||
"""
|
||||
def __init__(self, irc_params: tuple, v5_params: tuple, channels: list, prefix: str=""):
|
||||
def __init__(self, irc_params: tuple, channels: list, prefix: str=""):
|
||||
"""Initialize the Bot instance.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
irc_params : tuple
|
||||
Contains the IRC server informations (host, port)
|
||||
v5_params : tuple
|
||||
Contains the V5 server informations (host, port)
|
||||
channels : list
|
||||
Contains the names of the channels on which the bot will connect.
|
||||
prefix : str, optionnal
|
||||
|
@ -78,7 +75,6 @@ class Bot:
|
|||
PREFIX = prefix
|
||||
|
||||
self.irc = IRC(*irc_params)
|
||||
self.v5 = V5(v5_params, self.irc)
|
||||
self.channels = channels
|
||||
|
||||
def start(self):
|
||||
|
@ -90,9 +86,6 @@ class Bot:
|
|||
for channel in self.channels:
|
||||
self.irc.join(channel)
|
||||
|
||||
# Start V5 hadndler
|
||||
self.v5.start()
|
||||
|
||||
# Run IRC
|
||||
self.irc.run()
|
||||
|
|
@ -1,92 +0,0 @@
|
|||
"""
|
||||
v5 (GLaDOS)
|
||||
===========
|
||||
|
||||
Description
|
||||
-----------
|
||||
Manage the V5 layer of GLaDOS.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import socket
|
||||
from threading import Thread
|
||||
from functools import wraps
|
||||
|
||||
|
||||
class V5:
|
||||
"""Manage connexion beetween the bot and the V5 server, and manage callbacks.
|
||||
|
||||
Attributes
|
||||
----------
|
||||
irc : irc.IRC, public
|
||||
An IRC instance.
|
||||
|
||||
sock : ssl.SSLSocket, private
|
||||
The V5 socket.
|
||||
handler : Thread, private
|
||||
callbacks : list, private
|
||||
List of the registred callbacks.
|
||||
|
||||
Methods
|
||||
-------
|
||||
start : NoneType, public
|
||||
Start v5 handler.
|
||||
on : function, public
|
||||
Add a callback to the v5 handler.
|
||||
|
||||
handle : NoneType, private
|
||||
Handle the incoming messages and callbacks.
|
||||
"""
|
||||
|
||||
def __init__(self, v5_params: tuple, irc):
|
||||
"""Initialize V5 handle.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
v5 : tuple
|
||||
The information on V5 server (host, port).
|
||||
irc : irc.IRC
|
||||
An initialized IRC instance.
|
||||
"""
|
||||
self.irc = irc
|
||||
self.__sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
|
||||
self.__sock.bind(v5_params)
|
||||
self.__handler = Thread(target=self.__handle)
|
||||
self.__callbacks = []
|
||||
|
||||
def start(self):
|
||||
"""Start v5 handler."""
|
||||
self.__handler.start()
|
||||
logging.info("started")
|
||||
|
||||
def on(self, event):
|
||||
"""Adds a callback to the v5 handler.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
event : function
|
||||
``event`` is a function taking in parameter a list of channels and a string, and return
|
||||
``True`` if the callback should be executed.
|
||||
"""
|
||||
def callback(func):
|
||||
@wraps(func)
|
||||
def wrapper(channels, message):
|
||||
func(channels, message)
|
||||
self.__callbacks.append((event, wrapper))
|
||||
return wrapper
|
||||
|
||||
return callback
|
||||
|
||||
def __handle(self):
|
||||
"""Handle the incoming messages and callbacks."""
|
||||
while True:
|
||||
data, addr = self.__sock.recvfrom(4096)
|
||||
data = data.decode()
|
||||
logging.debug("received %s", data)
|
||||
channels, message = data.split(":", 1)
|
||||
channels = channels.split(" ")
|
||||
|
||||
for event, callback in self.__callbacks:
|
||||
if event(channels, message):
|
||||
logging.info("passed %s", event.__name__)
|
||||
callback(channels, message)
|
8
main.py
8
main.py
|
@ -10,7 +10,7 @@ Create a bot's instance and manages it.
|
|||
|
||||
import logging
|
||||
import re
|
||||
from irc_api import commands
|
||||
from irc_api import api
|
||||
from glados_cmnds import GladosV4, GladosV5
|
||||
|
||||
|
||||
|
@ -18,9 +18,8 @@ LOG_FORMAT = "%(asctime)s [%(levelname)s] <%(filename)s> %(funcName)s: %(message
|
|||
logging.basicConfig(format=LOG_FORMAT, level=logging.DEBUG)
|
||||
|
||||
|
||||
glados = commands.Bot(
|
||||
glados = api.Bot(
|
||||
('irc.planet-casio.com', 6697),
|
||||
('127.0.0.1', 5555),
|
||||
["#general", "#glados"],
|
||||
"!"
|
||||
)
|
||||
|
@ -31,7 +30,4 @@ glados.add_commands_pack(GladosV5)
|
|||
glados.add_help()
|
||||
|
||||
|
||||
for i in glados.irc.callbacks:
|
||||
print(i.name)
|
||||
|
||||
glados.start()
|
||||
|
|
Loading…
Reference in New Issue