Parce que Python c'est bien, en faire exécuter par les bots c'est bon esprit.
This commit is contained in:
parent
53310ede4e
commit
6cc3993f3c
18 changed files with 72 additions and 8 deletions
|
@ -45,8 +45,15 @@ bots:
|
||||||
- "Tu sais {user}, si j'suis toujours si bien accompagné, c'est pour mon argent hein, c'est pas pour mon odeur... Et ça ça fait mal"
|
- "Tu sais {user}, si j'suis toujours si bien accompagné, c'est pour mon argent hein, c'est pas pour mon odeur... Et ça ça fait mal"
|
||||||
- "Ou tu sors, ou j'te sors, hein, mais faudra prendre une décision."
|
- "Ou tu sors, ou j'te sors, hein, mais faudra prendre une décision."
|
||||||
- "Et à propos de vieille truie toi comment ça va ?"
|
- "Et à propos de vieille truie toi comment ça va ?"
|
||||||
|
Chuck:
|
||||||
|
on_ping:
|
||||||
|
- "No, thanks."
|
||||||
|
on_ping_python:
|
||||||
|
- 'klafirc.bots.chuck_norris.on_ping'
|
||||||
|
|
||||||
channels:
|
channels:
|
||||||
- server: irc.rezometz.org
|
- server: irc.rezometz.org
|
||||||
port: 6667
|
port: 6667
|
||||||
channel: "#campus"
|
channel: "#campus"
|
||||||
bots: [sel, Macron, Patou, Claudy]
|
bots: [sel, Macron, Patou, Claudy, Chuck]
|
||||||
|
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -1,5 +1,6 @@
|
||||||
import re
|
import re
|
||||||
import random
|
import random
|
||||||
|
import importlib
|
||||||
|
|
||||||
class Bot:
|
class Bot:
|
||||||
def __init__(self, nickname):
|
def __init__(self, nickname):
|
||||||
|
@ -31,11 +32,30 @@ class Bot:
|
||||||
}
|
}
|
||||||
self.reactions[re.compile(match.format(**context))] = reaction
|
self.reactions[re.compile(match.format(**context))] = reaction
|
||||||
|
|
||||||
|
def add_python_reaction(self, match, reaction):
|
||||||
|
""" Add a Python callback to the reactions.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
match: The string which, if matched will trigger the answer.
|
||||||
|
reaction: The path to the callback
|
||||||
|
"""
|
||||||
|
self.add_reaction(match, self.fetch_callback(reaction))
|
||||||
|
|
||||||
def add_ping(self, reaction):
|
def add_ping(self, reaction):
|
||||||
"""Add a reaction to a ping"""
|
"""Add a reaction to a ping"""
|
||||||
|
|
||||||
self.pings.append(reaction)
|
self.pings.append(reaction)
|
||||||
|
|
||||||
|
def add_python_ping(self, reaction):
|
||||||
|
"""Fetch a Python callable and add it to the pings"""
|
||||||
|
self.add_ping(self.fetch_callback(reaction))
|
||||||
|
|
||||||
|
def fetch_callback(self, path):
|
||||||
|
"""Fetch a Python callable"""
|
||||||
|
s = path.split('.')
|
||||||
|
module, callback = '.'.join(s[:-1]), s[-1]
|
||||||
|
module = importlib.import_module(module)
|
||||||
|
return getattr(module, callback)
|
||||||
|
|
||||||
def get_reaction(self, user, channel, message):
|
def get_reaction(self, user, channel, message):
|
||||||
"""Get a reaction to a message.
|
"""Get a reaction to a message.
|
||||||
|
|
||||||
|
@ -62,11 +82,20 @@ class Bot:
|
||||||
result = []
|
result = []
|
||||||
for m in self.reactions.keys():
|
for m in self.reactions.keys():
|
||||||
if m.search(message):
|
if m.search(message):
|
||||||
result.append(self.reactions[m].format(**context))
|
r = self.reactions[m]
|
||||||
|
if callable(r):
|
||||||
|
r = r(self, username, channel, message)
|
||||||
|
else:
|
||||||
|
r = r.format(**context)
|
||||||
|
result.append(r)
|
||||||
|
|
||||||
if not result and self.ping_match.search(message):
|
if not result and self.ping_match.search(message):
|
||||||
sentence = random.choice(self.pings).format(**context)
|
r = random.choice(self.pings)
|
||||||
result.append(' : '.join([username, sentence]))
|
if callable(r):
|
||||||
|
r = r(self, username, channel, message)
|
||||||
|
else:
|
||||||
|
r = r.format(**context)
|
||||||
|
result.append(' : '.join([username, r]))
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
BIN
klafirc/bots/.__init__.py.swp
Normal file
BIN
klafirc/bots/.__init__.py.swp
Normal file
Binary file not shown.
BIN
klafirc/bots/.chuck_norris.py.swp
Normal file
BIN
klafirc/bots/.chuck_norris.py.swp
Normal file
Binary file not shown.
1
klafirc/bots/__init__.py
Normal file
1
klafirc/bots/__init__.py
Normal file
|
@ -0,0 +1 @@
|
||||||
|
from . import chuck_norris
|
BIN
klafirc/bots/__pycache__/__init__.cpython-36.pyc
Normal file
BIN
klafirc/bots/__pycache__/__init__.cpython-36.pyc
Normal file
Binary file not shown.
BIN
klafirc/bots/__pycache__/chuck_norris.cpython-36.pyc
Normal file
BIN
klafirc/bots/__pycache__/chuck_norris.cpython-36.pyc
Normal file
Binary file not shown.
13
klafirc/bots/chuck_norris.py
Normal file
13
klafirc/bots/chuck_norris.py
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
"""
|
||||||
|
Fetch a random chuck norris fact and dislays it.
|
||||||
|
"""
|
||||||
|
import requests
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
def get_content():
|
||||||
|
r = requests.get('https://api.chucknorris.io/jokes/random')
|
||||||
|
return json.loads(r.content)['value']
|
||||||
|
|
||||||
|
def on_ping(bot, user, channel, message):
|
||||||
|
return get_content()
|
|
@ -19,10 +19,14 @@ class Loader:
|
||||||
b.port = port
|
b.port = port
|
||||||
for ping in template.get('on_ping', []):
|
for ping in template.get('on_ping', []):
|
||||||
b.add_ping(ping)
|
b.add_ping(ping)
|
||||||
|
for ping in template.get('on_ping_python', []):
|
||||||
|
b.add_python_ping(ping)
|
||||||
|
|
||||||
matches = template.get('on_match', [])
|
matches = template.get('on_match', [])
|
||||||
for match in matches:
|
for match in matches:
|
||||||
b.add_reaction(match, matches[match])
|
b.add_reaction(match, matches[match])
|
||||||
|
for match in template.get('on_match_python', []):
|
||||||
|
b.add_python_reaction(match, matches[match])
|
||||||
|
|
||||||
return b
|
return b
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,14 @@
|
||||||
import logging
|
import logging
|
||||||
from logging.handlers import RotatingFileHandler
|
from logging.handlers import RotatingFileHandler
|
||||||
|
|
||||||
DEBUG = True
|
DEBUG = False
|
||||||
BOT_FILE = '/etc/klafirc/bots.yaml'
|
|
||||||
LOG_FILE = '/var/log/klafirc/klafirc.log'
|
if not DEBUG:
|
||||||
|
BOT_FILE = '/etc/klafirc/bots.yaml'
|
||||||
|
LOG_FILE = '/var/log/klafirc/klafirc.log'
|
||||||
|
else:
|
||||||
|
BOT_FILE = './bots.yaml'
|
||||||
|
LOG_FILE = './klafirc.log'
|
||||||
|
|
||||||
logger = logging.getLogger()
|
logger = logging.getLogger()
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,16 @@
|
||||||
attrs==18.1.0
|
attrs==18.1.0
|
||||||
Automat==0.7.0
|
Automat==0.7.0
|
||||||
|
certifi==2018.4.16
|
||||||
|
chardet==3.0.4
|
||||||
constantly==15.1.0
|
constantly==15.1.0
|
||||||
hyperlink==18.0.0
|
hyperlink==18.0.0
|
||||||
idna==2.7
|
idna==2.7
|
||||||
incremental==17.5.0
|
incremental==17.5.0
|
||||||
|
Klafirc==0.1
|
||||||
PyHamcrest==1.9.0
|
PyHamcrest==1.9.0
|
||||||
PyYAML==3.13
|
PyYAML==3.13
|
||||||
|
requests==2.19.1
|
||||||
six==1.11.0
|
six==1.11.0
|
||||||
Twisted==18.7.0
|
Twisted==18.7.0
|
||||||
|
urllib3==1.23
|
||||||
zope.interface==4.5.0
|
zope.interface==4.5.0
|
||||||
|
|
Loading…
Reference in a new issue