poezio/plugins/cyber.py

43 lines
955 B
Python
Raw Permalink Normal View History

2015-05-11 21:35:47 +00:00
"""
This plugin adds a "cyber" prefix to a random word in your chatroom messages.
Usage
-----
Say something in a MUC tab.
Configuration options
---------------------
.. glossary::
frequency
**Default:** ``10``
The percentage of the time the plugin will activate (randomly). 100 for every message, <= 0 for never.
"""
from poezio.plugin import BasePlugin
2015-05-11 21:35:47 +00:00
from random import choice, randint
import re
DEFAULT_CONFIG = {'cyber': {'frequency': 10}}
2018-08-15 11:13:17 +00:00
2015-05-11 21:35:47 +00:00
class Plugin(BasePlugin):
default_config = DEFAULT_CONFIG
def init(self):
self.api.add_event_handler('muc_say', self.cyberize)
def cyberize(self, msg, tab):
if randint(1, 100) > self.config.get('frequency'):
return
2018-08-15 11:13:17 +00:00
words = [
word for word in re.split('\W+', msg['body']) if len(word) > 3
]
2015-05-11 21:35:47 +00:00
if words:
word = choice(words)
msg['body'] = msg['body'].replace(word, 'cyber' + word)