forked from Mrinank-Bhowmick/python-beginner-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
89 lines (65 loc) · 2.85 KB
/
main.py
File metadata and controls
89 lines (65 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# build a simple discord bot
import discord
from discord.ext import commands # importing commands module from discord extensions
import os
import random
token = "Your Token Here"
bot = discord.bot(comand_prefix="!")
# The list below is a list that contains all the responses the bot can randomly choose from for the "ask" command
# Scroll down to see the "ask" command's functionality
responses_list = [
"It is certain",
"It is decidedly so",
"Without a doubt",
"Yes, definitely",
"You may rely on it",
"As I see it, yes",
"Most likely",
"Outlook good",
"Yes",
"Signs point to yes",
"Reply hazy try again",
"Ask again later",
"Better not tell you now",
"Cannot predict now",
"Concentrate and ask again",
"Don't count on it",
"My reply is no",
"My sources say no",
"Outlook not so good",
"Very doubtful",
]
@bot.event
async def on_ready():
print("Bot is ready")
@bot.event
async def on_message(message: discord.Message):
if message.author == bot.user:
return
if message.content.startswith("!hello"):
await message.channel.send(f"Hello {message.author.mention}!")
return
if message.content.startswith("!random"):
await message.channel.send(random.randint(1, 100))
# adding a "@bot.command" decorator which creates a command for the discord bot which can be then invoked by the user
# "aliases" parameter inside the "@bot.command" decorator makes it so that the user can use different names to call
# that particular command
@bot.command(aliases=["ASK", "Ask"])
async def ask(ctx: commands.Context, *, question: str):
# Note:- If we don't put a ( * ) before the question paramter, the bot will only take the first word from the user
# input. For example: Running the command like this:- "!ask how are you?"
# The bot will read that command as:- "how"
await ctx.reply(
f"{ctx.author.mention} asks: **{question}**\nMy reply: **{random.choice(responses_list)}**"
)
# There's one last thing to do now.. which is handling error. As we can see, the "ask" commands needs a question
# parameter.. but what if the user just uses the command and never provide the bot with the question parameter?
# This situation will throw an error called "MissingRequiredArgument". In order to avoid this, we can locally
# create a error handler for this "ask" command. You can also use a try and except block to catch the error.
@ask.error
async def ask_error(ctx: commands.Context, error):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.reply("You didn't provide me with a question!")
# the above error handler checks specifically for the error "MissingRequiredArgument". If the command encounters with
# this error, the bot will just reply to the user's message with the sentence pasted above in the error handler
bot.run(token)