|
1 | 1 | # build a simple discord bot |
2 | 2 |
|
3 | 3 | import discord |
| 4 | +from discord.ext import commands # importing commands module from discord extensions |
4 | 5 | import os |
5 | 6 | import random |
6 | 7 |
|
7 | 8 | token = "Your Token Here" |
8 | 9 |
|
9 | 10 | bot = discord.bot(comand_prefix="!") |
10 | 11 |
|
| 12 | +# The list below is a list that contains all the responses the bot can randomly choose from for the "ask" command |
| 13 | +# Scroll down to see the "ask" command's functionality |
| 14 | + |
| 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"] |
| 35 | + |
11 | 36 |
|
12 | 37 | @bot.event |
13 | 38 | async def on_ready(): |
14 | 39 | print("Bot is ready") |
15 | 40 |
|
16 | 41 |
|
17 | 42 | @bot.event |
18 | | -async def on_message(message): |
| 43 | +async def on_message(message: discord.Message): |
19 | 44 | if message.author == bot.user: |
20 | 45 | return |
21 | 46 |
|
22 | 47 | if message.content.startswith("!hello"): |
23 | | - await message.channel.send("Hello!") |
| 48 | + await message.channel.send(f"Hello {message.author.mention}!") |
| 49 | + return |
24 | 50 |
|
25 | 51 | if message.content.startswith("!random"): |
26 | 52 | await message.channel.send(random.randint(1, 100)) |
27 | 53 |
|
28 | 54 |
|
| 55 | +# adding a "@bot.command" decorator which creates a command for the discord bot which can be then invoked by the user |
| 56 | +# "aliases" parameter inside the "@bot.command" decorator makes it so that the user can use different names to call |
| 57 | +# that particular command |
| 58 | + |
| 59 | +@bot.command(aliases = ["ASK", "Ask"]) |
| 60 | +async def ask(ctx: commands.Context, *, question: str): |
| 61 | + |
| 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}**\nMy reply: **{random.choice(responses_list)}**") |
| 67 | + |
| 68 | +# There's one last thing to do now.. which is handling error. As we can see, the "ask" commands needs a question |
| 69 | +# parameter.. but what if the user just uses the command and never provide the bot with the question parameter? |
| 70 | +# This situation will throw an error called "MissingRequiredArgument". In order to avoid this, we can locally |
| 71 | +# create a error handler for this "ask" command. You can also use a try and except block to catch the error. |
| 72 | + |
| 73 | +@ask.error |
| 74 | +async def ask_error(ctx: commands.Context, error): |
| 75 | + if isinstance(error, commands.MissingRequiredArgument): |
| 76 | + await ctx.reply("You didn't provide me with a question!") |
| 77 | + |
| 78 | +# the above error handler checks specifically for the error "MissingRequiredArgument". If the command encounters with |
| 79 | +# this error, the bot will just reply to the user's message with the sentence pasted above in the error handler |
| 80 | + |
29 | 81 | bot.run(token) |
0 commit comments