Skip to content

Commit 7668b48

Browse files
committed
add error handling to poll function
1 parent 7fb985f commit 7668b48

File tree

1 file changed

+60
-57
lines changed

1 file changed

+60
-57
lines changed

protobot.py

Lines changed: 60 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ async def about(ctx):
298298
await ctx.message.channel.send(f"ProtoBot {VERSION}. Source code and bug tracker: https://github.com/klercke/ProtoBot")
299299

300300

301-
@bot.command(name="poll", help="Creates a poll. Usage: !poll \"PROMPT\" TIME OPTION1 OPTION2 [OPTION3...]")
301+
@bot.command(name="poll", help="Creates a poll. Usage: !poll \"QUESTION\" TIME[S/M/H/D] EMOJI1 EMOJI2 ...")
302302
async def poll(ctx):
303303
"""
304304
Allows users to create timed polls
@@ -307,65 +307,68 @@ async def poll(ctx):
307307
user_input = ctx.message.content
308308
user = ctx.message.author
309309

310-
# Get the user's question
311-
prompt = ""
312-
writing = False
313-
count = 0
314-
for character in user_input:
315-
count += 1
316-
if not writing and character == "\"":
317-
writing = True
318-
elif writing:
319-
if character == "\"":
320-
writing = False
321-
break
322-
else:
323-
prompt += character
324-
elif count == len(user_input) - 1:
325-
await ctx.channel.send("Sorry, I couldn't understand your command. Please make sure the poll question is in quotes.")
326-
return
327-
328-
# Get the TTL for the poll
329-
user_input = user_input[count + 1:]
330-
if not user_input:
331-
await ctx.channel.send("You didn't provide a time. Assuming 5 minutes.")
332-
poll_time = "5m"
333-
else:
334-
poll_time = ""
310+
try:
311+
# Get the user's question
312+
prompt = ""
313+
writing = False
335314
count = 0
336315
for character in user_input:
337316
count += 1
338-
if character != " ":
339-
poll_time += character
340-
else:
341-
break
342-
343-
unit_long = ""
344-
unit = poll_time[-1].lower()
345-
poll_time = int(poll_time[:-1])
346-
poll_time_in_sec = 0
347-
if unit == 's':
348-
unit_long = "seconds"
349-
poll_time_in_sec = poll_time
350-
elif unit == 'm':
351-
unit_long = "minutes"
352-
poll_time_in_sec = poll_time * 60
353-
elif unit == 'h':
354-
unit_long = "hours"
355-
poll_time_in_sec = poll_time * 3600
356-
elif unit == 'd':
357-
unit_long = "days"
358-
poll_time_in_sec = poll_time * 86400
359-
360-
361-
# Get emoji options
362-
user_input = user_input[count:]
363-
user_input = user_input.split()
364-
options = []
365-
message_sent = await ctx.message.channel.send(f"<@{user.id}> has started a poll:\n{prompt}\nVoting will last {poll_time} {unit_long}.")
366-
for emoji in user_input:
367-
options += emoji
368-
await message_sent.add_reaction(emoji)
317+
if not writing and character == "\"":
318+
writing = True
319+
elif writing:
320+
if character == "\"":
321+
writing = False
322+
break
323+
else:
324+
prompt += character
325+
elif count == len(user_input) - 1:
326+
await ctx.channel.send("Sorry, I couldn't understand your command. Please make sure the poll question is in quotes.")
327+
return
328+
329+
# Get the TTL for the poll
330+
user_input = user_input[count + 1:]
331+
if not user_input:
332+
await ctx.channel.send("You didn't provide a time. Assuming 5 minutes.")
333+
poll_time = "5m"
334+
else:
335+
poll_time = ""
336+
count = 0
337+
for character in user_input:
338+
count += 1
339+
if character != " ":
340+
poll_time += character
341+
else:
342+
break
343+
344+
unit_long = ""
345+
unit = poll_time[-1].lower()
346+
poll_time = int(poll_time[:-1])
347+
poll_time_in_sec = 0
348+
if unit == 's':
349+
unit_long = "seconds"
350+
poll_time_in_sec = poll_time
351+
elif unit == 'm':
352+
unit_long = "minutes"
353+
poll_time_in_sec = poll_time * 60
354+
elif unit == 'h':
355+
unit_long = "hours"
356+
poll_time_in_sec = poll_time * 3600
357+
elif unit == 'd':
358+
unit_long = "days"
359+
poll_time_in_sec = poll_time * 86400
360+
361+
362+
# Get emoji options
363+
user_input = user_input[count:]
364+
user_input = user_input.split()
365+
options = []
366+
message_sent = await ctx.message.channel.send(f"<@{user.id}> has started a poll:\n{prompt}\nVoting will last {poll_time} {unit_long}.")
367+
for emoji in user_input:
368+
options += emoji
369+
await message_sent.add_reaction(emoji)
370+
except:
371+
await ctx.message.channel.send(f"<@{user.id}>, something went wrong with your command. Please make sure to use proper syntax:\n!poll \"QUESTION\" TIME[S/M/H/D] EMOJI1 EMOJI2 ...")
369372

370373
async def count_poll_results(message_sent, poll_time_in_sec):
371374
# Wait for voting to finish

0 commit comments

Comments
 (0)