Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 5f208c3

Browse files
committed
Made some changes to the on_message event in main.py and added a new basic command
1 parent 451ad51 commit 5f208c3

1 file changed

Lines changed: 66 additions & 2 deletions

File tree

projects/Discord Bot/main.py

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,93 @@
11
# build a simple discord bot
22

33
import discord
4+
from discord.ext import commands # importing commands module from discord extensions
45
import os
56
import random
67

78
token = "Your Token Here"
89

910
bot = discord.bot(comand_prefix="!")
1011

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+
1138

1239
@bot.event
1340
async def on_ready():
1441
print("Bot is ready")
1542

1643

1744
@bot.event
18-
async def on_message(message):
45+
async def on_message(message: discord.Message):
1946
if message.author == bot.user:
2047
return
2148

2249
if message.content.startswith("!hello"):
23-
await message.channel.send("Hello!")
50+
await message.channel.send(f"Hello {message.author.mention}!")
51+
return
2452

2553
if message.content.startswith("!random"):
2654
await message.channel.send(random.randint(1, 100))
2755

2856

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+
2993
bot.run(token)

0 commit comments

Comments
 (0)