Whisper back on whispered commands

master
Nikola Forró 7 years ago
parent ef68689900
commit c75ee6f957

@ -1,4 +1,5 @@
import configparser
import functools
import logging
import os
import random
@ -83,6 +84,12 @@ class TwitchBot(irc.bot.SingleServerIRCBot):
return None
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):
connection.cap('REQ', ':twitch.tv/membership')
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}
message = ''.join([c for c in event.arguments[0] if c in string.printable])
message = message.rstrip()
respond = functools.partial(self._respond, connection, event)
for pattern, action in self.patterns + self.commands:
m = pattern.match(message)
if m:
action(connection, tags, **m.groupdict())
action(tags, respond, **m.groupdict())
def get(self, 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()
return r
def bellagram(self, connection, tags, **kwargs):
def bellagram(self, tags, respond, **kwargs):
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:
msg = random.choice(self.bellagrams)['url']
connection.privmsg(self.channel, msg)
respond(random.choice(self.bellagrams)['url'])
def last_quote(self, connection, tags, **kwargs):
def last_quote(self, tags, respond, **kwargs):
try:
quotes = self.get(dict(
sort_by='id',
@ -140,15 +147,13 @@ class TwitchBot(irc.bot.SingleServerIRCBot):
page_size=1)).json()
quote = quotes[0]
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:
msg = '!quote {0}'.format(quote['id'])
connection.privmsg(self.channel, msg)
respond('!quote {0}'.format(quote['id']))
def find_quote(self, connection, tags, filter, **kwargs):
def find_quote(self, tags, respond, filter, **kwargs):
if len(filter) < 3:
msg = 'Sorry @{0}, the search phrase is too short'.format(tags['display-name'])
connection.privmsg(self.channel, msg)
respond('Sorry @{0}, the search phrase is too short'.format(tags['display-name']))
return
try:
quotes = self.get(dict(
@ -157,15 +162,13 @@ class TwitchBot(irc.bot.SingleServerIRCBot):
page_size=1)).json()
quote = quotes[0]
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:
msg = '!quote {0}'.format(quote['id'])
connection.privmsg(self.channel, msg)
respond('!quote {0}'.format(quote['id']))
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:
msg = 'Sorry @{0}, you are not allowed to do this'.format(tags['display-name'])
connection.privmsg(self.channel, msg)
respond('Sorry @{0}, you are not allowed to do this'.format(tags['display-name']))
return
if since is None:
try:
@ -194,10 +197,9 @@ class TwitchBot(irc.bot.SingleServerIRCBot):
m = pattern.match(message)
if m:
action(connection, None, **m.groupdict())
msg = '@{0}, sync completed'.format(tags['display-name'])
connection.privmsg(self.channel, msg)
respond('@{0}: sync completed'.format(tags['display-name']))
def query_youtube(self, connection, tags, query, **kwargs):
def query_youtube(self, tags, respond, query, **kwargs):
api_key = config['Youtube'].get('api_key')
channel_id = config['Youtube'].get('channel_id')
yt = Youtube(api_key)
@ -205,12 +207,11 @@ class TwitchBot(irc.bot.SingleServerIRCBot):
if not items:
items = yt.search(channel_id, query, playlists=False, limit=1)
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:
msg = '{0} - {1}'.format(items[0]['title'], items[0]['url'])
connection.privmsg(self.channel, msg)
respond('{0}: {1}'.format(items[0]['title'], items[0]['url']))
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] == '"':
text = text[1:-1]
log.info('Adding quote %s: %s', id, text)
@ -223,7 +224,7 @@ class TwitchBot(irc.bot.SingleServerIRCBot):
except requests.exceptions.HTTPError as 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] == '"':
text = text[1:-1]
log.info('Editing quote %s: %s', id, text)
@ -236,7 +237,7 @@ class TwitchBot(irc.bot.SingleServerIRCBot):
except requests.exceptions.HTTPError as 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)
try:
self.delete(int(id))

Loading…
Cancel
Save