-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
138 lines (110 loc) · 4.75 KB
/
bot.py
File metadata and controls
138 lines (110 loc) · 4.75 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
from discord.ext import commands
from mathjspy import MathJS
import re
import wolframalpha
import datetime
import discord
appid = ""
with open("WA_APPID.txt", "r") as appid_file:
appid = appid_file.read()
wa_client = wolframalpha.Client(appid)
mjs = MathJS()
todo_list = []
command_prefixes = ('homie ', '!')
bot = commands.Bot(command_prefix=command_prefixes, case_insensitive=True, help_command=None)
@bot.event
async def on_message(message):
if message.author.bot:
# Check if message is by another bot or itself
if (message.author == bot.user and
re.match('^`\d+\.\s',message.content)):
# Add reaction to todo list
await message.add_reaction('✅')
return
content_without_prefix = message.content
for command_prefix in command_prefixes:
# Remove command prefix if applicable
if content_without_prefix.lower().startswith(command_prefix):
content_without_prefix = remove_prefix(message.content, command_prefix)
break
if content_without_prefix.lower().startswith("solve"):
# Evaluate with mathjspy library
try:
await message.channel.send(mjs.eval(message.content[len("solve"):]))
except:
await message.channel.send("Sorry, we could not compute.")
elif content_without_prefix.lower().startswith("ask"):
# Query with wolframalpha library
async with message.channel.typing():
res = wa_client.query(message.content[len("ask"):])
answer = next(res.results).text
await message.channel.send(answer)
elif content_without_prefix.lower().startswith("add todo"):
# Add to todo list
todo_list.append(message.content[len("add todo"):])
message.content = "!todo"
await bot.process_commands(message)
@bot.event
async def on_reaction_add(reaction, user):
if user.bot:
return
if reaction.emoji == '✅' and reaction.message.author == bot.user:
# If someone checked something off of the todo list
todo_item = re.sub('^`\d+\.\s', '', reaction.message.content)
todo_item = todo_item[:len(todo_item) - 1]
if todo_item != reaction.message.content and todo_item in todo_list:
todo_list.remove(todo_item)
await reaction.message.add_reaction('🎉')
await reaction.message.channel.send(("Congrats on finishing " +
todo_item + "!!!"))
@bot.command(name = "todo")
async def todo(ctx):
if not todo_list:
await ctx.send("There is nothing on your todo list!")
else:
for num, item in enumerate(todo_list, 1):
await ctx.send(make_codeblock(str(num) + ". " + item))
await ctx.send(make_codeblock("React to check off a task!"))
@bot.command(name='info', aliases=['help', 'commands'])
async def await_info(ctx):
desc = 'Command prefixes (homie or !) are optional ' \
'on commands: solve, ask, and add todo. It is mandatory on all other commands.'
embed = discord.Embed(title='Homie W. Commands', description=desc)
embed.add_field(name='solve [equation]', value='Solve a math equation')
embed.add_field(name='ask [question]', value='Ask any question')
embed.add_field(name='todo', value='Display TODO List')
embed.add_field(name='add todo [todo list item]', value='Add an item to the TODO List')
embed.add_field(name='timer [seconds] [minutes] [hours] [days]', value='Set a timer')
embed.set_footer(text='Have a nice day!')
await ctx.send(embed=embed)
@bot.command(name = "timer")
async def timer(ctx, seconds: float, minutes: float=0, hours: float=0, days : float=0):
target = (datetime.datetime.now() + datetime.timedelta(days=days,
seconds=seconds, minutes=minutes, hours=hours))
timer_embed = discord.Embed(
title = "Timer"
)
timer_embed.add_field(
name = "Time Left:",
value = str(1)
)
timer_embed_message = await ctx.send(embed=timer_embed)
while datetime.datetime.now() < target:
time_left = target - datetime.datetime.now()
timer_embed.remove_field(0)
timer_embed.add_field(
name = "Time Left:",
value = str(time_left)
)
await timer_embed_message.edit(embed=timer_embed)
await ctx.send("Your timer is up!!", mention_author=True)
await ctx.send(minutes)
def make_codeblock(string : str):
return "`" + string + "`"
def remove_prefix(string:str, prefix:str):
if string.startswith(prefix):
return string[len(prefix):]
with open("BOT_TOKEN.txt", "r") as token_file:
TOKEN = token_file.read()
print("Token file read")
bot.run(TOKEN)