Pass tags instead of source and allow only master user to do !sync

master
Nikola Forró 7 years ago
parent c0ca339442
commit e091ae5638

@ -48,6 +48,7 @@ class TwitchBot(irc.bot.SingleServerIRCBot):
self.nickname = config['IRC'].get('nickname') self.nickname = config['IRC'].get('nickname')
self.channel = '#{0}'.format(config['IRC'].get('channel')) self.channel = '#{0}'.format(config['IRC'].get('channel'))
self.token = config['Twitch'].get('token') self.token = config['Twitch'].get('token')
self.master_user_id = config['Twitch'].getint('master_user_id')
self.api_url = config['Quotes'].get('api_url') self.api_url = config['Quotes'].get('api_url')
self.api_key = config['Quotes'].get('api_key') self.api_key = config['Quotes'].get('api_key')
log.info('Connecting to %s:%d', self.server, self.port) log.info('Connecting to %s:%d', self.server, self.port)
@ -71,13 +72,13 @@ class TwitchBot(irc.bot.SingleServerIRCBot):
self.process_message(connection, event) self.process_message(connection, event)
def process_message(self, connection, event): def process_message(self, connection, event):
sources = [t['value'] for t in event.tags if t['key'] == 'display-name'] 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()
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, sources[0], **m.groupdict()) action(connection, tags, **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)
@ -96,7 +97,7 @@ class TwitchBot(irc.bot.SingleServerIRCBot):
r.raise_for_status() r.raise_for_status()
return r.json() return r.json()
def last_quote(self, connection, source, **kwargs): def last_quote(self, connection, tags, **kwargs):
try: try:
quotes = self.get(dict( quotes = self.get(dict(
sort_by='id', sort_by='id',
@ -104,14 +105,14 @@ class TwitchBot(irc.bot.SingleServerIRCBot):
page_size=1)) page_size=1))
quote = quotes[0] quote = quotes[0]
except (requests.exceptions.HTTPError, IndexError): except (requests.exceptions.HTTPError, IndexError):
msg = 'Sorry @{0}, no quotes found'.format(source) msg = 'Sorry @{0}, no quotes found'.format(tags['display-name'])
else: else:
msg = '!quote {0}'.format(quote['id']) msg = '!quote {0}'.format(quote['id'])
connection.privmsg(self.channel, msg) connection.privmsg(self.channel, msg)
def find_quote(self, connection, source, filter, **kwargs): def find_quote(self, connection, tags, filter, **kwargs):
if len(filter) < 3: if len(filter) < 3:
msg = 'Sorry @{0}, the search phrase is too short'.format(source) msg = 'Sorry @{0}, the search phrase is too short'.format(tags['display-name'])
connection.privmsg(self.channel, msg) connection.privmsg(self.channel, msg)
return return
try: try:
@ -122,12 +123,16 @@ class TwitchBot(irc.bot.SingleServerIRCBot):
page_size=1)) page_size=1))
quote = quotes[0] quote = quotes[0]
except (requests.exceptions.HTTPError, IndexError): except (requests.exceptions.HTTPError, IndexError):
msg = 'Sorry @{0}, no quotes found'.format(source) msg = 'Sorry @{0}, no quotes found'.format(tags['display-name'])
else: else:
msg = '!quote {0}'.format(quote['id']) msg = '!quote {0}'.format(quote['id'])
connection.privmsg(self.channel, msg) connection.privmsg(self.channel, msg)
def sync(self, connection, source, since=None, **kwargs): def sync(self, connection, tags, 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)
return
if since is None: if since is None:
try: try:
quotes = self.get(dict( quotes = self.get(dict(
@ -145,16 +150,16 @@ class TwitchBot(irc.bot.SingleServerIRCBot):
since = quote['date'] since = quote['date']
api_url = config['Twitch'].get('api_url') api_url = config['Twitch'].get('api_url')
client_id = config['Twitch'].get('client_id') client_id = config['Twitch'].get('client_id')
user_id = config['Twitch'].get('target_user_id') user_id = config['Twitch'].getint('target_user_id')
since = dateutil.parser.parse(since).date() since = dateutil.parser.parse(since).date()
messages = Twitch(api_url, client_id, log).get_messages(user_id, since) messages = Twitch(api_url, client_id, log).get_messages(user_id, since)
for message in messages: for message in messages:
for pattern, action in self.patterns: for pattern, action in self.patterns:
m = pattern.match(message) m = pattern.match(message)
if m: if m:
action(connection, m.group('user'), **m.groupdict()) action(connection, None, **m.groupdict())
def add_quote(self, connection, source, user, id, text, game, date, **kwargs): def add_quote(self, connection, tags, user, id, text, game, date, **kwargs):
log.info('Adding quote %s: %s', id, text) log.info('Adding quote %s: %s', id, text)
try: try:
self.post(dict( self.post(dict(

Loading…
Cancel
Save