|
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 | +# 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 | + |
| 17 | +responses_list = ["It is certain", |
| 18 | + "It is decidedly so", |
| 19 | + "Without a doubt", |
| 20 | + "Yes, definitely", |
| 21 | + "You may rely on it", |
| 22 | + "As I see it, yes", |
| 23 | + "Most likely", |
| 24 | + "Outlook good", |
| 25 | + "Yes", |
| 26 | + "Signs point to yes", |
| 27 | + "Reply hazy try again", |
| 28 | + "Ask again later", |
| 29 | + "Better not tell you now", |
| 30 | + "Cannot predict now", |
| 31 | + "Concentrate and ask again", |
| 32 | + "Don't count on it", |
| 33 | + "My reply is no", |
| 34 | + "My sources say no", |
| 35 | + "Outlook not so good", |
| 36 | + "Very doubtful"] |
| 37 | + |
11 | 38 |
|
12 | 39 | @bot.event |
13 | 40 | async def on_ready(): |
14 | 41 | print("Bot is ready") |
15 | 42 |
|
16 | 43 |
|
17 | 44 | @bot.event |
18 | | -async def on_message(message): |
| 45 | +async def on_message(message: discord.Message): |
19 | 46 | if message.author == bot.user: |
20 | 47 | return |
21 | 48 |
|
22 | 49 | if message.content.startswith("!hello"): |
23 | | - await message.channel.send("Hello!") |
| 50 | + await message.channel.send(f"Hello {message.author.mention}!") |
| 51 | + return |
24 | 52 |
|
25 | 53 | if message.content.startswith("!random"): |
26 | 54 | await message.channel.send(random.randint(1, 100)) |
27 | 55 |
|
28 | 56 |
|
| 57 | +# adding a "@bot.command" decorator which creates a command for the discord bot which can be then invoked by the user |
| 58 | +# "aliases" parameter inside the "@bot.command" decorator makes it so that the user can use multiple names to call |
| 59 | +# that particular command |
| 60 | + |
| 61 | +@bot.command(aliases = ["ASK", "Ask"]) |
| 62 | +async def ask(ctx: commands.Context, *, question: str): |
| 63 | + |
| 64 | +# Note:- If we don't put a ( * ) before the question paramter, the bot will only take the first word from the user |
| 65 | +# 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?". |
| 68 | + |
| 69 | + await ctx.reply(f"{ctx.author.mention} asks: **{question}**\nMy reply: **{random.choice(responses_list)}**") |
| 70 | + |
| 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 | +# There's one last thing to do now.. which is handling error. As we can see, the "ask" commands needs a question |
| 81 | +# 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 |
| 83 | +# create a error handler for this "ask" command. You can also use a try and except block to catch the error. |
| 84 | + |
| 85 | +@ask.error |
| 86 | +async def ask_error(ctx: commands.Context, error): |
| 87 | + if isinstance(error, commands.MissingRequiredArgument): |
| 88 | + await ctx.reply("You didn't provide me with a question!") |
| 89 | + |
| 90 | +# the above error handler checks specifically for the error "MissingRequiredArgument". If the command encounters with |
| 91 | +# this error, the bot will just reply to the user's message with the sentence pasted above in the error handler |
| 92 | + |
29 | 93 | bot.run(token) |
0 commit comments