import logging
import re

from bot.plugins.base import BasePlugin

from .models import GameNotification


logger = logging.getLogger(__name__)


class Plugin(BasePlugin):

    has_blocking_io = True
    subscribe_pattern = re.compile(r'!subscribe (?P<game>.+)', re.IGNORECASE)
    unsubscribe_pattern = re.compile(r'!unsubscribe (?P<game>.+)', re.IGNORECASE)
    channel = 'general'

    help = (
        '`!subscribe <game>` sets up notifications for that game\n'
        '`!unsubscribe <game>` deletes your subscription\n'
        '`!unsubscribe !all` deletes all your subscriptions\n'
        'Commands are case-insensitive'
    )

    def _member_active(self, member):
        return member.status in ['online', 'idle'] and not member.is_afk and not member.game

    def on_member_update(self, before, after):
        if after.game and after.game != before.game:
            member = after
            game = member.game.name
            subscribers = GameNotification.objects.filter(game_name__iexact=game).exclude(user=member.id)
            if not subscribers.exists():
                return

            ids = subscribers.values_list('user', flat=True)
            members = [m for m in member.server.members if m.id in ids and self._member_active(m)]
            if not members:
                return

            mentions = ', '.join([m.mention() for m in members])
            msg = '{mentions}: {name} started playing {game}'.format(mentions=mentions, name=member.name, game=game)
            channel = next((c for c in member.server.channels if c.name == self.channel), None)
            yield from self.client.send_message(channel, msg)

    def on_message(self, message):
        user = message.author.id

        match = re.match(self.subscribe_pattern, message.content)
        if match:
            game = match.group('game').strip()
            notification, created = GameNotification.objects.get_or_create(game_name=game.lower(), user=user)
            msg = 'Subscribed you to {game}' if created else 'You were already subscribed to {game}'
            yield from self.client.send_message(message.channel, msg.format(game=game))
            return

        match = re.match(self.unsubscribe_pattern, message.content)
        if match:
            game = match.group('game').strip()
            if game.lower() == '!all':
                deleted, _ = GameNotification.objects.filter(user=user).delete()
                yield from self.client.send_message(
                    message.channel,
                    'Unsubscribed you from {num} games'.format(num=deleted)
                )
                return

            deleted, _ = GameNotification.objects.filter(user=user, game_name__iexact=game).delete()
            if deleted:
                yield from self.client.send_message(
                    message.channel,
                    'Unsubscribed you from {game} games'.format(game=game)
                )
            return

@@ -19,16 +19,50 @@
 ort re%0A%0A
+from discord.enums import Status%0A%0A
 from bot
@@ -696,24 +696,72 @@
 f, member):%0A
+        statuses = %5BStatus.online, Status.idle%5D%0A
         retu
@@ -784,26 +784,16 @@
  in 
-%5B'online', 'idle'%5D
+statuses
  and

Fix status enums check