Whisper back on whispered commands

master
Nikola Forró 7 years ago
parent ef68689900
commit c75ee6f957

@ -1,4 +1,5 @@
import configparser import configparser
import functools
import logging import logging
import os import os
import random import random
@ -83,6 +84,12 @@ class TwitchBot(irc.bot.SingleServerIRCBot):
return None return None
return [m for m in media if [k for k in keywords if k.lower() in m['text'].lower()]] return [m for m in media if [k for k in keywords if k.lower() in m['text'].lower()]]
def _respond(self, connection, event, msg):
if event.target.startswith('#'):
connection.privmsg(event.target, msg)
else:
connection.privmsg('#jtv', '/w {0} {1}'.format(event.source.nick, msg))
def on_welcome(self, connection, event): def on_welcome(self, connection, event):
connection.cap('REQ', ':twitch.tv/membership') connection.cap('REQ', ':twitch.tv/membership')
connection.cap('REQ', ':twitch.tv/tags') connection.cap('REQ', ':twitch.tv/tags')
@ -103,10 +110,11 @@ class TwitchBot(irc.bot.SingleServerIRCBot):
tags = {t['key']: t['value'] for t in event.tags} tags = {t['key']: t['value'] for t in event.tags}
message = ''.join([c for c in event.arguments[0] if c in string.printable]) message = ''.join([c for c in event.arguments[0] if c in string.printable])
message = message.rstrip() message = message.rstrip()
respond = functools.partial(self._respond, connection, event)
for pattern, action in self.patterns + self.commands: for pattern, action in self.patterns + self.commands:
m = pattern.match(message) m = pattern.match(message)
if m: if m:
action(connection, tags, **m.groupdict()) action(tags, respond, **m.groupdict())
def get(self, params): def get(self, params):
r = requests.get('{0}/quotes'.format(self.api_url), params=params) r = requests.get('{0}/quotes'.format(self.api_url), params=params)
@ -125,14 +133,13 @@ class TwitchBot(irc.bot.SingleServerIRCBot):
r.raise_for_status() r.raise_for_status()
return r return r
def bellagram(self, connection, tags, **kwargs): def bellagram(self, tags, respond, **kwargs):
if not self.bellagrams: if not self.bellagrams:
msg = 'Sorry @{0}, couldn\'t get any media from Instagram'.format(tags['display-name']) respond('Sorry @{0}, couldn\'t get any media from Instagram'.format(tags['display-name']))
else: else:
msg = random.choice(self.bellagrams)['url'] respond(random.choice(self.bellagrams)['url'])
connection.privmsg(self.channel, msg)
def last_quote(self, connection, tags, **kwargs): def last_quote(self, tags, respond, **kwargs):
try: try:
quotes = self.get(dict( quotes = self.get(dict(
sort_by='id', sort_by='id',
@ -140,15 +147,13 @@ class TwitchBot(irc.bot.SingleServerIRCBot):
page_size=1)).json() page_size=1)).json()
quote = quotes[0] quote = quotes[0]
except (requests.exceptions.HTTPError, IndexError): except (requests.exceptions.HTTPError, IndexError):
msg = 'Sorry @{0}, no quotes found'.format(tags['display-name']) respond('Sorry @{0}, no quotes found'.format(tags['display-name']))
else: else:
msg = '!quote {0}'.format(quote['id']) respond('!quote {0}'.format(quote['id']))
connection.privmsg(self.channel, msg)
def find_quote(self, connection, tags, filter, **kwargs): def find_quote(self, tags, respond, filter, **kwargs):
if len(filter) < 3: if len(filter) < 3:
msg = 'Sorry @{0}, the search phrase is too short'.format(tags['display-name']) respond('Sorry @{0}, the search phrase is too short'.format(tags['display-name']))
connection.privmsg(self.channel, msg)
return return
try: try:
quotes = self.get(dict( quotes = self.get(dict(
@ -157,15 +162,13 @@ class TwitchBot(irc.bot.SingleServerIRCBot):
page_size=1)).json() page_size=1)).json()
quote = quotes[0] quote = quotes[0]
except (requests.exceptions.HTTPError, IndexError): except (requests.exceptions.HTTPError, IndexError):
msg = 'Sorry @{0}, no quotes found'.format(tags['display-name']) respond('Sorry @{0}, no quotes found'.format(tags['display-name']))
else: else:
msg = '!quote {0}'.format(quote['id']) respond('!quote {0}'.format(quote['id']))
connection.privmsg(self.channel, msg)
def sync(self, connection, tags, since=None, **kwargs): def sync(self, tags, respond, since=None, **kwargs):
if int(tags['user-id']) != self.master_user_id: if int(tags['user-id']) != self.master_user_id:
msg = 'Sorry @{0}, you are not allowed to do this'.format(tags['display-name']) respond('Sorry @{0}, you are not allowed to do this'.format(tags['display-name']))
connection.privmsg(self.channel, msg)
return return
if since is None: if since is None:
try: try:
@ -194,10 +197,9 @@ class TwitchBot(irc.bot.SingleServerIRCBot):
m = pattern.match(message) m = pattern.match(message)
if m: if m:
action(connection, None, **m.groupdict()) action(connection, None, **m.groupdict())
msg = '@{0}, sync completed'.format(tags['display-name']) respond('@{0}: sync completed'.format(tags['display-name']))
connection.privmsg(self.channel, msg)
def query_youtube(self, connection, tags, query, **kwargs): def query_youtube(self, tags, respond, query, **kwargs):
api_key = config['Youtube'].get('api_key') api_key = config['Youtube'].get('api_key')
channel_id = config['Youtube'].get('channel_id') channel_id = config['Youtube'].get('channel_id')
yt = Youtube(api_key) yt = Youtube(api_key)
@ -205,12 +207,11 @@ class TwitchBot(irc.bot.SingleServerIRCBot):
if not items: if not items:
items = yt.search(channel_id, query, playlists=False, limit=1) items = yt.search(channel_id, query, playlists=False, limit=1)
if not items: if not items:
msg = 'Sorry @{0}, couldn\'t find anything on Youtube'.format(tags['display-name']) respond('Sorry @{0}, couldn\'t find anything on Youtube'.format(tags['display-name']))
else: else:
msg = '{0} - {1}'.format(items[0]['title'], items[0]['url']) respond('{0}: {1}'.format(items[0]['title'], items[0]['url']))
connection.privmsg(self.channel, msg)
def add_quote(self, connection, tags, user, id, text, game, date, **kwargs): def add_quote(self, tags, respond, user, id, text, game, date, **kwargs):
if text[0] == text[-1] == '"': if text[0] == text[-1] == '"':
text = text[1:-1] text = text[1:-1]
log.info('Adding quote %s: %s', id, text) log.info('Adding quote %s: %s', id, text)
@ -223,7 +224,7 @@ class TwitchBot(irc.bot.SingleServerIRCBot):
except requests.exceptions.HTTPError as e: except requests.exceptions.HTTPError as e:
log.error('Failed to add quote: %s', str(e)) log.error('Failed to add quote: %s', str(e))
def edit_quote(self, connection, tags, user, id, text, game, date, **kwargs): def edit_quote(self, tags, respond, user, id, text, game, date, **kwargs):
if text[0] == text[-1] == '"': if text[0] == text[-1] == '"':
text = text[1:-1] text = text[1:-1]
log.info('Editing quote %s: %s', id, text) log.info('Editing quote %s: %s', id, text)
@ -236,7 +237,7 @@ class TwitchBot(irc.bot.SingleServerIRCBot):
except requests.exceptions.HTTPError as e: except requests.exceptions.HTTPError as e:
log.error('Failed to edit quote: %s', str(e)) log.error('Failed to edit quote: %s', str(e))
def remove_quote(self, connection, tags, user, id, **kwargs): def remove_quote(self, tags, respond, user, id, **kwargs):
log.info('Removing quote %s', id) log.info('Removing quote %s', id)
try: try:
self.delete(int(id)) self.delete(int(id))

Loading…
Cancel
Save