|
11 | 11 |
|
12 | 12 | # The list below is a list that contains all the responses the bot can randomly choose from for the "ask" command |
13 | 13 | # Scroll down to see the "ask" command's functionality |
14 | | -# It is recommended to make this list in a different .txt file or .py file and then importing it to this main.py |
15 | | -# file but for the sake of simplicity, I have added this list here only in the main bot file. |
16 | 14 |
|
17 | 15 | responses_list = ["It is certain", |
18 | 16 | "It is decidedly so", |
@@ -63,23 +61,13 @@ async def ask(ctx: commands.Context, *, question: str): |
63 | 61 |
|
64 | 62 | # Note:- If we don't put a ( * ) before the question paramter, the bot will only take the first word from the user |
65 | 63 | # input. For example: Running the command like this:- "!ask how are you?" |
66 | | -# The bot will read that command as:- "how". To avoid this, we add an asterisk( * ) before the question parameter |
67 | | -# so the bot will read the command as "how are you?". |
| 64 | +# The bot will read that command as:- "how" |
68 | 65 |
|
69 | 66 | await ctx.reply(f"{ctx.author.mention} asks: **{question}**\nMy reply: **{random.choice(responses_list)}**") |
70 | 67 |
|
71 | | -# the ctx object has an attribute called "reply" which will reply to the user's message when the user uses to command |
72 | | -# prefix along with the command name. And the rest of the code is self explanatory. The "\n" basically makes it so |
73 | | -# that the rest of the code after that "\n" will be printed in a new line. So ultimately, running this command |
74 | | -# on discord will look like this:- |
75 | | - |
76 | | - # lets say user asks the bot "how are you". So the user will send: "!ask how are you". |
77 | | - # the bot will reply with "{user's discord id mention} asks: how are you" |
78 | | - # "My reply: {a random response from the responses list}" |
79 | | - |
80 | 68 | # There's one last thing to do now.. which is handling error. As we can see, the "ask" commands needs a question |
81 | 69 | # parameter.. but what if the user just uses the command and never provide the bot with the question parameter? |
82 | | -# This situation will through an error called "MissingRequiredArgument". In order to avoid this, we can locally |
| 70 | +# This situation will throw an error called "MissingRequiredArgument". In order to avoid this, we can locally |
83 | 71 | # create a error handler for this "ask" command. You can also use a try and except block to catch the error. |
84 | 72 |
|
85 | 73 | @ask.error |
|
0 commit comments