Fix handling of giveaways

master
Nikola Forró 6 years ago
parent 86e06d20b0
commit 78c189ceff

@ -60,6 +60,16 @@ GIVEAWAY_STARTED_PATTERN = re.compile(r'''^
(?P<prize>.+)\s+has\s+started!\s+
(?P<entitled>.+)\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<prize>.+)\s+
(?P<entitled>.+)\s+can\s+[jJ]oin!?$''', re.VERBOSE)
# Type $command to join!
GIVEAWAY_COMMAND_PATTERN = re.compile(r'''^
Type\s+(?P<command>.+)\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):

Loading…
Cancel
Save