41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
"""Observe Markov chains from Discord."""
|
|
import logging
|
|
|
|
from django.conf import settings
|
|
|
|
from bot import hitomi
|
|
from markov import lib
|
|
|
|
logger = logging.getLogger(__name__)
|
|
logger.info("loading markov plugin")
|
|
|
|
|
|
def on_message(message):
|
|
"""Keep the observed Markov chains."""
|
|
# ignore self
|
|
if message.author == hitomi.user:
|
|
logger.debug("markov ignoring message authored by self")
|
|
return
|
|
|
|
# ignore commands
|
|
if message.content[0] == settings.DISCORD_BOT_COMMAND_PREFIX:
|
|
logger.debug("markov ignoring message that looks like a command")
|
|
return
|
|
|
|
if message.channel.is_private:
|
|
# DMs have a context of the author
|
|
context_name = message.author
|
|
else:
|
|
# channels have a context of the server, but we should ignore channels that override @everyone's read ability
|
|
for changed_role in message.channel.changed_roles:
|
|
if changed_role.is_everyone:
|
|
if not changed_role.permissions.read_messages:
|
|
logger.debug("markov ignoring channel that @everyone can't read")
|
|
return
|
|
context_name = message.server.id
|
|
|
|
lib.learn_line(message.content, context_name)
|
|
|
|
|
|
hitomi.on_message_handlers.append(on_message)
|