11# build a simple discord bot
22
33import discord
4- from discord .ext import commands # importing commands module from discord extensions
4+ from discord .ext import commands # importing commands module from discord extensions
55import os
66import random
77
1212# The list below is a list that contains all the responses the bot can randomly choose from for the "ask" command
1313# Scroll down to see the "ask" command's functionality
1414
15- responses_list = ["It is certain" ,
16- "It is decidedly so" ,
17- "Without a doubt" ,
18- "Yes, definitely" ,
19- "You may rely on it" ,
20- "As I see it, yes" ,
21- "Most likely" ,
22- "Outlook good" ,
23- "Yes" ,
24- "Signs point to yes" ,
25- "Reply hazy try again" ,
26- "Ask again later" ,
27- "Better not tell you now" ,
28- "Cannot predict now" ,
29- "Concentrate and ask again" ,
30- "Don't count on it" ,
31- "My reply is no" ,
32- "My sources say no" ,
33- "Outlook not so good" ,
34- "Very doubtful" ]
15+ responses_list = [
16+ "It is certain" ,
17+ "It is decidedly so" ,
18+ "Without a doubt" ,
19+ "Yes, definitely" ,
20+ "You may rely on it" ,
21+ "As I see it, yes" ,
22+ "Most likely" ,
23+ "Outlook good" ,
24+ "Yes" ,
25+ "Signs point to yes" ,
26+ "Reply hazy try again" ,
27+ "Ask again later" ,
28+ "Better not tell you now" ,
29+ "Cannot predict now" ,
30+ "Concentrate and ask again" ,
31+ "Don't count on it" ,
32+ "My reply is no" ,
33+ "My sources say no" ,
34+ "Outlook not so good" ,
35+ "Very doubtful" ,
36+ ]
3537
3638
3739@bot .event
@@ -56,25 +58,31 @@ async def on_message(message: discord.Message):
5658# "aliases" parameter inside the "@bot.command" decorator makes it so that the user can use different names to call
5759# that particular command
5860
59- @bot .command (aliases = ["ASK" , "Ask" ])
61+
62+ @bot .command (aliases = ["ASK" , "Ask" ])
6063async def ask (ctx : commands .Context , * , question : str ):
6164
62- # Note:- If we don't put a ( * ) before the question paramter, the bot will only take the first word from the user
63- # input. For example: Running the command like this:- "!ask how are you?"
64- # The bot will read that command as:- "how"
65-
66- await ctx .reply (f"{ ctx .author .mention } asks: **{ question } **\n My reply: **{ random .choice (responses_list )} **" )
65+ # Note:- If we don't put a ( * ) before the question paramter, the bot will only take the first word from the user
66+ # input. For example: Running the command like this:- "!ask how are you?"
67+ # The bot will read that command as:- "how"
68+
69+ await ctx .reply (
70+ f"{ ctx .author .mention } asks: **{ question } **\n My reply: **{ random .choice (responses_list )} **"
71+ )
72+
6773
6874# There's one last thing to do now.. which is handling error. As we can see, the "ask" commands needs a question
6975# parameter.. but what if the user just uses the command and never provide the bot with the question parameter?
7076# This situation will throw an error called "MissingRequiredArgument". In order to avoid this, we can locally
7177# create a error handler for this "ask" command. You can also use a try and except block to catch the error.
72-
78+
79+
7380@ask .error
7481async def ask_error (ctx : commands .Context , error ):
7582 if isinstance (error , commands .MissingRequiredArgument ):
7683 await ctx .reply ("You didn't provide me with a question!" )
7784
85+
7886# the above error handler checks specifically for the error "MissingRequiredArgument". If the command encounters with
7987# this error, the bot will just reply to the user's message with the sentence pasted above in the error handler
8088
0 commit comments