From fe3b482e123343625b19f248d3a0e87755020466 Mon Sep 17 00:00:00 2001 From: Lint Action Date: Sat, 10 Feb 2024 21:26:07 +0000 Subject: [PATCH] Fix code style issues with Black --- projects/Discord Bot/main.py | 64 ++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/projects/Discord Bot/main.py b/projects/Discord Bot/main.py index cbe571a2..3b4a7f70 100644 --- a/projects/Discord Bot/main.py +++ b/projects/Discord Bot/main.py @@ -1,7 +1,7 @@ # build a simple discord bot import discord -from discord.ext import commands # importing commands module from discord extensions +from discord.ext import commands # importing commands module from discord extensions import os import random @@ -12,26 +12,28 @@ # The list below is a list that contains all the responses the bot can randomly choose from for the "ask" command # Scroll down to see the "ask" command's functionality -responses_list = ["It is certain", - "It is decidedly so", - "Without a doubt", - "Yes, definitely", - "You may rely on it", - "As I see it, yes", - "Most likely", - "Outlook good", - "Yes", - "Signs point to yes", - "Reply hazy try again", - "Ask again later", - "Better not tell you now", - "Cannot predict now", - "Concentrate and ask again", - "Don't count on it", - "My reply is no", - "My sources say no", - "Outlook not so good", - "Very doubtful"] +responses_list = [ + "It is certain", + "It is decidedly so", + "Without a doubt", + "Yes, definitely", + "You may rely on it", + "As I see it, yes", + "Most likely", + "Outlook good", + "Yes", + "Signs point to yes", + "Reply hazy try again", + "Ask again later", + "Better not tell you now", + "Cannot predict now", + "Concentrate and ask again", + "Don't count on it", + "My reply is no", + "My sources say no", + "Outlook not so good", + "Very doubtful", +] @bot.event @@ -56,25 +58,31 @@ async def on_message(message: discord.Message): # "aliases" parameter inside the "@bot.command" decorator makes it so that the user can use different names to call # that particular command -@bot.command(aliases = ["ASK", "Ask"]) + +@bot.command(aliases=["ASK", "Ask"]) async def ask(ctx: commands.Context, *, question: str): -# Note:- If we don't put a ( * ) before the question paramter, the bot will only take the first word from the user -# input. For example: Running the command like this:- "!ask how are you?" -# The bot will read that command as:- "how" - - await ctx.reply(f"{ctx.author.mention} asks: **{question}**\nMy reply: **{random.choice(responses_list)}**") + # Note:- If we don't put a ( * ) before the question paramter, the bot will only take the first word from the user + # input. For example: Running the command like this:- "!ask how are you?" + # The bot will read that command as:- "how" + + await ctx.reply( + f"{ctx.author.mention} asks: **{question}**\nMy reply: **{random.choice(responses_list)}**" + ) + # There's one last thing to do now.. which is handling error. As we can see, the "ask" commands needs a question # parameter.. but what if the user just uses the command and never provide the bot with the question parameter? # This situation will throw an error called "MissingRequiredArgument". In order to avoid this, we can locally # create a error handler for this "ask" command. You can also use a try and except block to catch the error. - + + @ask.error async def ask_error(ctx: commands.Context, error): if isinstance(error, commands.MissingRequiredArgument): await ctx.reply("You didn't provide me with a question!") + # the above error handler checks specifically for the error "MissingRequiredArgument". If the command encounters with # this error, the bot will just reply to the user's message with the sentence pasted above in the error handler