forked from IEEE-VIT/concy-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
174 lines (135 loc) · 6.01 KB
/
bot.py
File metadata and controls
174 lines (135 loc) · 6.01 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import discord
from discord.ext import commands, tasks
import asyncio
import urllib.request
import json
from dotenv import dotenv_values
config = dotenv_values(".env")
client = commands.Bot(command_prefix=".")
@client.event
async def on_ready():
print("Bot is ready")
@client.command(aliases=["timer", "start-timer"])
async def start_timer(ctx, seconds):
if not seconds.isnumeric():
await ctx.send("Must be a number!")
# Convert string (user input command) into an integer
seconds = int(seconds)
if seconds <= 0:
await ctx.send("I don't think I'm allowed to do negatives")
# Send a message to the Discord channel
await ctx.send(f"Your countdown has been started for {str(seconds)} seconds")
# We are making a variable, so that we can reference our message later on
# and thus, edit our message on the Discord Server
message = await ctx.send(f"Time Remaining : {str(seconds)} second(s)")
while True:
seconds -= 1
if seconds == 0:
await message.edit(content=(f"Time Remaining : 0"))
# Notify the author (user who issued the command) using ctx.message.author.mention
await ctx.send(ctx.message.author.mention + " Your countdown has ended!")
break
# Thus, we edit our message on the Discord channel with the current remaining time
await message.edit(content=(f"Time Remaining : {str(seconds)} second(s)"))
# Sleep for 1 second
await asyncio.sleep(1)
@client.command(aliases=["set-alarm"])
async def alarm(ctx, time):
# Take user input (time in 24-hour format), for example 23:00
# Check if it is a valid 24-hour format, and return an apppropriate message
# Parse the user input using time.split(":")
# Remind the user as soon as it's time
pass
@client.command(aliases=["stopwatch", "start-stopwatch"])
async def stopwach(ctx):
# Start a stopwatch
# Wait for the stop command from user (You may use "client.wait_for")
pass
@client.command(aliases=["hourly-reminder", "set-hourly-reminder"])
async def hourly_reminder(ctx, task):
# Takes input from the user (task) about what they would like to accomplish
# Send a reminder after one hour to see how far they have come up with the task
# At the end of every hour, ask the user if they have completed the task.
# Wait for the stop command from user (You may use "client.wait_for")
pass
@client.command(aliases=["daily-reminder", "set-daily-alarm"])
async def daily_reminder(ctx,*, task):
# Takes input from the user (task) about what they would like to accomplish
await ctx.send(ctx.author.mention + f" Task: {task}, saved successfully")
# Start reminder loop
reminder_loop.start(ctx, task)
@tasks.loop(minutes=60)
async def reminder_loop(ctx, task):
# Send reminder and ask for confirmation
await ctx.send(ctx.author.mention + f" Task: \"{task}\", should be completed today, \nHave done it already?")
# Check message source
def check(m):
return m.channel == ctx.channel and m.author == ctx.author
# Wait for user reply to cancel loop
try:
reply = await client.wait_for("message", check=check, timeout=300)
if reply.content.lower() in ["stop", "yes", "yep", "done", "sure"]:
await ctx.send(ctx.author.mention + f" Congratulations, you finished your daily task: \n\t\"{task}\"")
reminder_loop.cancel()
else:
await ctx.send(ctx.author.mention + " Okay, I'll remind you again in an hour")
# Continue in case of timeout
except Exception:
await ctx.send(ctx.author.mention + " I'll remind you again in an hour")
@client.command(aliases=["start-pomodoro"])
async def pomodoro(ctx):
# Set a timer for 25 minutes
time = 1500
# Notify the user after 25 minutes
message = await ctx.send("Your countdown has been started for 25 minutes")
while True:
time -= 1
if time == 0:
await ctx.send(ctx.message.author.mention + " your break has started.")
break
await asyncio.sleep(1)
# Set the timer for 5 minutes (starting of the break)
time = 300
await message.edit(content=("Your break countdown has been started for 5 minutes"))
while True:
time -= 1
if time == 0:
break
await asyncio.sleep(1)
# Notify the user after 5 minutes that Pomodoro has ended
await ctx.send(ctx.message.author.mention + " Pomodoro has ended!")
def getQuote(tags=["inspirational", "success"]): # default arguments
#add tags to the url
url = "https://api.quotable.io/random?tags="
for tag in tags:
url = url+tag+"|"
# get json response from the quoteable api
response = urllib.request.urlopen(url).read()
# Convert json response into a dictionary
response_dict = json.loads(response)
quote_author = response_dict["author"]
quote_text = response_dict["content"]
return (quote_text,quote_author)
@client.command(aliases=["quote","motivation"])
async def motivational_quote(ctx,*tags):
# tags that are available in the quoteable api
AVAILABLE_TAGS = ['business', 'education', 'faith', 'famous-quotes', 'friendship', 'future', 'happiness',
'history', 'inspirational', 'life', 'literature', 'love', 'nature', 'politics', 'proverb',
'religion', 'science', 'success', 'technology', 'wisdom']
# check if the tags enterend as arguments are valid
quote = ()
if len(tags) > 0:
if set(tags).issubset(set(AVAILABLE_TAGS)):
quote = getQuote(tags=tags)
else:
await ctx.send("Invalid tag\nthe available tags are:\n"+", ".join(AVAILABLE_TAGS))
return
else:
quote = getQuote()
quote_text = "\"" + quote[0] + "\""
quote_author = "-" + quote[1]
# Make a discord embed with quote
quote_embed = discord.Embed(title="Motivational Quote",description=quote_text,)
quote_embed.set_footer(text=quote_author)
await ctx.send(embed=quote_embed)
client.run(config["token"])