From 78c189ceff0d54c7deb40be99a97616658e3d694 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Forr=C3=B3?= Date: Sat, 5 Jan 2019 18:46:32 +0100 Subject: [PATCH] Fix handling of giveaways --- clients/twitch.py | 46 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/clients/twitch.py b/clients/twitch.py index 32f0a94..6e856b2 100644 --- a/clients/twitch.py +++ b/clients/twitch.py @@ -60,6 +60,16 @@ GIVEAWAY_STARTED_PATTERN = re.compile(r'''^ (?P.+)\s+has\s+started!\s+ (?P.+)\s+can\s+[jJ]oin!?$''', re.VERBOSE) +# Entries are allowed once again for $prize $entitled can join! +GIVEAWAY_RESTARTED_PATTERN = re.compile(r'''^ + Entries\s+are\s+allowed\s+once\s+again\s+ + for\s+(?P.+)\s+ + (?P.+)\s+can\s+[jJ]oin!?$''', re.VERBOSE) + +# Type $command to join! +GIVEAWAY_COMMAND_PATTERN = re.compile(r'''^ + Type\s+(?P.+)\s+to\s+join!$''', re.VERBOSE) + # Entries have stopped for the giveaway! You can no longer enter! GIVEAWAY_ENDED_PATTERN = re.compile(r'''^ Entries\s+have\s+stopped\s+for\s+the\s+giveaway!\s+ @@ -81,6 +91,8 @@ class TwitchClient(irc.bot.SingleServerIRCBot): (RESUB_PATTERN, self._record_resub), (SUB_GIFTED_PATTERN, self._record_gifted_sub), (GIVEAWAY_STARTED_PATTERN, self._process_giveaway_start), + (GIVEAWAY_RESTARTED_PATTERN, self._process_giveaway_restart), + (GIVEAWAY_COMMAND_PATTERN, self._process_giveaway_command), (GIVEAWAY_ENDED_PATTERN, self._process_giveaway_end), ] self.supported_commands = [ @@ -205,10 +217,28 @@ class TwitchClient(irc.bot.SingleServerIRCBot): def _process_giveaway_start(self, tags, send_response, prize, entitled, **kwargs): starttime = kwargs.get('time', datetime.datetime.utcnow()) - self.giveaway = (starttime, prize, entitled) + self.giveaway = dict( + active=True, + starttime=starttime, + prize=prize, + entitled=entitled, + command=None) + + def _process_giveaway_restart(self, tags, send_response, prize, entitled, **kwargs): + if not self.giveaway: + self._process_giveaway_start(tags, send_response, prize, entitled, **kwargs) + return + self.giveaway['active'] = True + + def _process_giveaway_command(self, tags, send_response, command, **kwargs): + if not self.giveaway: + return + self.giveaway['command'] = command def _process_giveaway_end(self, tags, send_response, **kwargs): - self.giveaway = None + if not self.giveaway: + return + self.giveaway['active'] = False def _do_lastquote(self, tags, send_response, **kwargs): try: @@ -289,17 +319,19 @@ class TwitchClient(irc.bot.SingleServerIRCBot): send_response('{0}: {1}'.format(result['title'], result['url'])) def _do_giveaway(self, tags, send_response, **kwargs): - if not self.giveaway: + if not self.giveaway or not self.giveaway['active']: send_response('There is currently no giveaway in progress') return - starttime, prize, entitled = self.giveaway - elapsed = (datetime.datetime.utcnow() - starttime).total_seconds() + elapsed = (datetime.datetime.utcnow() - self.giveaway['starttime']).total_seconds() if elapsed > 2 * 60 * 60: - self.giveaway = None + self.giveaway['active'] = False send_response('There is currently no giveaway in progress') return elapsed = time.strftime('%H:%M', time.gmtime(elapsed)) - send_response('Giveaway for {0} is open (for {1})! {2} can join!'.format(prize, elapsed, entitled)) + send_response('Giveaway for {0} is open (for {1})! {2} can join!'.format( + self.giveaway['prize'], elapsed, self.giveaway['entitled'])) + if self.giveaway['command']: + send_response('Type {0} to join!'.format(self.giveaway['command'])) def _do_command_set(self, tags, send_response, cmd, resp, **kwargs): if not self._is_mod(tags):