-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmess_management.py
93 lines (90 loc) · 4 KB
/
mess_management.py
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
import discord
import calendar, datetime, json
import utils
async def send_menu(ctx, client, args):
hostels = []
days = []
ck = 0
for arg in args:
if type(args) == discord.member.Member:
try:
kerberos = json.load(open("datafiles/discord_ids.json"))[str(args.id)]["kerberos"]
hostels.append(utils.kerberos[kerberos]["hostel"])
except:
pass
else:
arg = arg.title()
if arg in utils.hostels:
hostels.append(arg)
elif arg.lower() == '-all':
days = [day.title() for day in utils.days]
ck = 1
elif arg[0] == '-' and ck == 0:
for d in utils.days:
if arg[1:4].lower() == d.lower():
days.append(d.title())
if len(days) == 0:
days.append(calendar.day_abbr[(datetime.datetime.utcnow()+datetime.timedelta(hours=5.5)).weekday()])
if len(hostels) == 0:
kerberos = json.load(open("datafiles/discord_ids.json"))[str(ctx.message.author.id)]["kerberos"]
hostel = utils.kerberos[kerberos]["hostel"]
if len(hostel) > 1:
hostels.append(hostel)
else:
await ctx.reply("Please provide a hostel for which you want to see the menu")
if len(hostels)*len(days) > 7:
await ctx.reply("Maximum 7 operations are allowed. Please try again with fewer arguments!")
return
ctr = 0
for hostel in hostels:
with open("datafiles/mess.json", "r") as f:
menu = json.load(f)
menu = menu[hostel]
for day in days:
today = menu[day]
embed = discord.Embed(title=f'{day}\'s Mess Menu for {hostel}', color=discord.Color.blue())
for meal in today:
embed.add_field(name=f"**{meal}** ({today[meal]['time']})", value=f"{today[meal]['menu']}", inline=False)
if ctr == 0:
ctr = 1
await ctx.reply(embed=embed)
else:
await ctx.send(embed=embed)
async def update_menu(ctx, client, hostel, day, meal, menu):
with open("datafiles/mess.json", "r") as f:
data = json.load(f)
last_menu = data[hostel][day][meal]['menu']
data[hostel][day][meal]['menu'] = menu
with open("datafiles/mess.json", "w") as f:
json.dump(data, f)
await ctx.reply("Updated the menu successfully!")
mess_channel = client.get_channel(1025322494606983188)
embed = discord.Embed(
description=f"**{meal} menu for {hostel} hostel was updated by {ctx.message.author.mention}**",
color=0x3480D5,
timestamp=datetime.datetime.utcnow()
)
embed.add_field(name="Previous Menu", value=last_menu, inline=False)
embed.add_field(name="New Menu", value=menu, inline=False)
embed.set_author(name=f"{ctx.message.author.name}#{ctx.message.author.discriminator}", icon_url=ctx.message.author.avatar_url)
embed.set_footer(text=f"ID: {ctx.message.author.id}")
await mess_channel.send(embed=embed)
async def update_time(ctx, client, hostel, day, meal, time):
with open("datafiles/mess.json", "r") as f:
data = json.load(f)
last_time = data[hostel][day][meal]['time']
data[hostel][day][meal]['time'] = time
with open("datafiles/mess.json", "w") as f:
json.dump(data, f)
await ctx.reply("Updated the timing successfully!")
mess_channel = client.get_channel(1025322494606983188)
embed = discord.Embed(
description=f"**{meal} timing for {hostel} hostel was updated by {ctx.message.author.mention}**",
color=0x3480D5,
timestamp=datetime.datetime.utcnow()
)
embed.add_field(name="Previous Timing", value=last_time, inline=False)
embed.add_field(name="New Timing", value=time, inline=False)
embed.set_author(name=f"{ctx.message.author.name}#{ctx.message.author.discriminator}", icon_url=ctx.message.author.avatar_url)
embed.set_footer(text=f"ID: {ctx.message.author.id}")
await mess_channel.send(embed=embed)