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

Commit ad1e956

Browse files
Merge pull request #701 from Xia3412/discord-bot-branch
Made changes to the discord bot project
2 parents 41490e7 + 356f258 commit ad1e956

1 file changed

Lines changed: 54 additions & 2 deletions

File tree

projects/Discord Bot/main.py

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,81 @@
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+
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+
1136

1237
@bot.event
1338
async def on_ready():
1439
print("Bot is ready")
1540

1641

1742
@bot.event
18-
async def on_message(message):
43+
async def on_message(message: discord.Message):
1944
if message.author == bot.user:
2045
return
2146

2247
if message.content.startswith("!hello"):
23-
await message.channel.send("Hello!")
48+
await message.channel.send(f"Hello {message.author.mention}!")
49+
return
2450

2551
if message.content.startswith("!random"):
2652
await message.channel.send(random.randint(1, 100))
2753

2854

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

0 commit comments

Comments
 (0)