topicmonitor: monitor when channel topic changes
comes with a migration to add topic tracking to IrcChannel
This commit is contained in:
parent
b15afcade8
commit
a3484b0d3a
48
dr_botzo/ircbot/ircplugins/topicmonitor.py
Normal file
48
dr_botzo/ircbot/ircplugins/topicmonitor.py
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
|
from ircbot.lib import Plugin
|
||||||
|
from ircbot.models import IrcChannel
|
||||||
|
|
||||||
|
|
||||||
|
log = logging.getLogger('ircbot.ircplugins.topicmonitor')
|
||||||
|
|
||||||
|
|
||||||
|
class TopicMonitor(Plugin):
|
||||||
|
|
||||||
|
"""Have IRC commands to do IRC things (join channels, quit, etc.)."""
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
"""Set up the handlers."""
|
||||||
|
|
||||||
|
self.connection.reactor.add_global_handler('topic', handle_topic, -20)
|
||||||
|
|
||||||
|
super(TopicMonitor, self).start()
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
"""Tear down handlers."""
|
||||||
|
|
||||||
|
self.connection.reactor.remove_global_handler('topic', handle_topic)
|
||||||
|
|
||||||
|
super(TopicMonitor, self).stop()
|
||||||
|
|
||||||
|
|
||||||
|
def handle_topic(connection, event):
|
||||||
|
"""Store topic changes in the channel model."""
|
||||||
|
|
||||||
|
channel = event.target
|
||||||
|
topic = event.arguments[0]
|
||||||
|
setter = event.source
|
||||||
|
log.debug("topic change '%s' by %s in %s", topic, setter, channel)
|
||||||
|
|
||||||
|
channel, c = IrcChannel.objects.get_or_create(name=channel)
|
||||||
|
channel.topic_msg = topic
|
||||||
|
channel.topic_time = timezone.now()
|
||||||
|
channel.topic_by = setter
|
||||||
|
channel.save()
|
||||||
|
|
||||||
|
|
||||||
|
plugin = TopicMonitor
|
30
dr_botzo/ircbot/migrations/0006_auto_20150515_2141.py
Normal file
30
dr_botzo/ircbot/migrations/0006_auto_20150515_2141.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django.db import models, migrations
|
||||||
|
import django.utils.timezone
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('ircbot', '0005_auto_20150514_2317'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='ircchannel',
|
||||||
|
name='topic_by',
|
||||||
|
field=models.CharField(default=b'', max_length=200),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='ircchannel',
|
||||||
|
name='topic_msg',
|
||||||
|
field=models.TextField(default=b''),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='ircchannel',
|
||||||
|
name='topic_time',
|
||||||
|
field=models.DateTimeField(default=django.utils.timezone.now),
|
||||||
|
),
|
||||||
|
]
|
@ -4,6 +4,7 @@ import logging
|
|||||||
import re
|
import re
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
|
|
||||||
log = logging.getLogger('ircbot.models')
|
log = logging.getLogger('ircbot.models')
|
||||||
@ -51,6 +52,10 @@ class IrcChannel(models.Model):
|
|||||||
name = models.CharField(max_length=200, unique=True)
|
name = models.CharField(max_length=200, unique=True)
|
||||||
autojoin = models.BooleanField(default=False)
|
autojoin = models.BooleanField(default=False)
|
||||||
|
|
||||||
|
topic_msg = models.TextField(default='')
|
||||||
|
topic_time = models.DateTimeField(default=timezone.now)
|
||||||
|
topic_by = models.CharField(max_length=200, default='')
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
"""String representation."""
|
"""String representation."""
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user