In the previous article, we embarked on the setup of our Discord Bot, Norgannon. We began by integrating the fundamental functionality necessary for Norgannon to successfully host an engaging Warcraft-based quiz game. Today, we’re ready to delve deeper and enhance the user experience with critical features of an online chat-based quiz. These include the introduction of a time-limited question timer, a fair point awarding system, and a error handling mechanism.
Challenges and Victories
Drawing on past experiences, and relying on common sense, it became evident that an online quiz game must provide a fair playing field for all participants. It cannot afford players with the fastest internet connection the luxury of time to Google answers. To ensure this, I established a 10-second time limit for each question. This timing provides just enough room for players to type in all but the longest of Warcraft dragon names, meticulously double-check their spelling, and then type it again, bringing a sense of urgency and thrill to the game.

To implement this feature, I imported the Python library asyncio. This common Python tool allowed me to use common time-based keywords and functions into my, and enabled the setting of my timer with a simple command, asyncio.sleep(10)
.
try:
msg = await bot.wait_for('message', check=check, timeout=10)
author = msg.author
Moving on to the point system, I discovered that it presented some additional nuance than I originally expected. Testing revealed errors and functional limitations that required some minor fiddling. However, I’m confident that once these glitches are patched, this system will perform as expected, assigning a single point to the first player who provides the correct answer within the allotted time limit.
To code this feature, I defined a library of questions and answers, player_scores
, which will store each player’s scores. It will then announce the final totals after the culmination of the game. To better understand the issues encountered, here’s the current non-functioning code for your amusement.
player_scores = {}
...
try:
msg = await bot.wait_for('message', check=check, timeout=10)
author = msg.author
if author.id not in player_scores:
player_scores[author.id] = 0
player_scores[author.id] += 1
await ctx.send(f"{msg.author.mention} got it right!")
...
await ctx.send("The quiz has ended. Here are the final scores:")
for player_id, score in player_scores.items():
player = bot.get_user(player_id)
await ctx.send(f"{player.mention}: {score} points")
Refining and Iterating
The strategic addition of the question timer called for the inclusion of a ‘try-except’ block in my code. This preventive measure ensures that in the event where no players could provide the correct answer within ten seconds, the resulting timeout error will not disrupt the continuation of the game or crash the bot. Instead, it explicitly instructs the program on how to handle the error in a more user-friendly manner: by informing the players that time is up, awarding no points, and smoothly transitioning to the next question.
try:
msg = await bot.wait_for('message', check=check, timeout=10)
author = msg.author
if author.id not in player_scores:
player_scores[author.id] = 0
player_scores[author.id] += 1
await ctx.send(f"{msg.author.mention} got it right!")
except asyncio.TimeoutError:
await ctx.send("Time is up! No one answered correctly.")
Reflections and Insights
Writing a second part of this series fills me with a sense of accomplishment. Often, I’ve found myself initiating projects and subsequently halting progress when faced with obstacles. This has evolved into a detrimental habit, fueled by the nagging voice of doubt whenever I venture into uncharted territories. Along with that, I’ve noticed my tendency to feel overwhelmed by the magnitude of complex projects and their completion timelines. Some primitive part of my brain craves immediate gratification, causing me to obsess over a project until I reach burnout. Yet this time, I’ve managed to pace myself, devoting small, manageable time-blocks each day to coding, and effectively restraining my impulsivity.
Looking Ahead
As I stride forward into the next phase of my project, my goal will be to refine the score-keeping feature and extend its capabilities to include a leaderboard. This leaderboard will keep track of overall wins across games, adding a layer of competitiveness and fostering a sense of community among players. This enhancement will likely necessitate the deployment of the bot in a hosted environment where a text file can be updated each time the game is played. Consequently, this could offer an ideal opportunity to learn about DevOps in the context of this project, aligning perfectly with one of my stretch goals for my programming journey.
Share Your Thoughts
Do you have any experiences that you’d like to share? Your insights can be a great help to others and myself!
Additionally, I’m always looking for new topics to cover in my blog. If you have a question about anything mentioned here, or if there’s a specific topic you’d like me to delve into, please let me know in the comments section below via my contact form. Your question could be the inspiration for our next blog post!