Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaincode.py
More file actions
Latest commit
92 lines (80 loc) · 3.62 KB
/
Copy pathmaincode.py
File metadata and controls
92 lines (80 loc) · 3.62 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
importdiscord
importasyncio
importdatetime
importpickle
fromdiscord.extimportcommands
intents=discord.Intents.all()
intents.members=True
bot=commands.Bot(command_prefix='!', intents=intents)
ALERTS_FILE='alerts.pickle'
try:
withopen(ALERTS_FILE, 'rb') asf:
alerts=pickle.load(f)
exceptFileNotFoundError:
alerts= {}
@bot.event
asyncdefon_ready():
print(f'Logged in as {bot.user}')
bot.loop.create_task(send_alerts())
@bot.event
asyncdefon_message(message):
ifmessage.content.startswith('!leppa'):
author_id=message.author.id
alerts[author_id] = {
'harvest': datetime.datetime.utcnow() +datetime.timedelta(hours=20)
}
awaitmessage.author.send('A reminder has been set to harvest your Leppa berries in 20 hours.')
withopen(ALERTS_FILE, 'wb') asf:
pickle.dump(alerts, f)
elifmessage.content.startswith('!water'):
try:
hours=int(message.content.split()[1])
author_id=message.author.id
ifauthor_idnotinalerts:
alerts[author_id] = {}
alerts[author_id]['water'] =datetime.datetime.utcnow() +datetime.timedelta(hours=hours)
awaitmessage.author.send(f'A reminder has been set to water your berries in {hours} hours.')
withopen(ALERTS_FILE, 'wb') asf:
pickle.dump(alerts, f)
except (IndexError, ValueError):
awaitmessage.author.send('Invalid syntax. Use !water (hours).')
elifmessage.content.startswith('!myalerts'):
author_id=message.author.id
ifauthor_idinalerts:
harvest_time=alerts[author_id].get('harvest')
water_time=alerts[author_id].get('water')
ifharvest_time:
awaitmessage.author.send(f'Harvest reminder in {(harvest_time-datetime.datetime.utcnow()).seconds//60} minutes.')
ifwater_time:
awaitmessage.author.send(f'Water reminder in {(water_time-datetime.datetime.utcnow()).seconds//60} minutes.')
else:
awaitmessage.author.send('You have no active reminders.')
elifmessage.content.startswith('!cancelalerts'):
author_id=message.author.id
ifauthor_idinalerts:
delalerts[author_id]
awaitmessage.author.send('All reminders cancelled.')
withopen(ALERTS_FILE, 'wb') asf:
pickle.dump(alerts, f)
else:
awaitmessage.author.send('You have no active reminders to cancel.')
asyncdefsend_alerts():
whileTrue:
now=datetime.datetime.utcnow()
forauthor_id, reminder_timesinalerts.copy().items():
user=bot.get_user(author_id)
ifuserisnotNone:
forreminder_type, reminder_timeinreminder_times.copy().items():
ifnow>=reminder_time:
awaituser.send(f'It is time to {reminder_type} your berries.')
ifreminder_type=='harvest':
delalerts[author_id]['harvest']
elifreminder_type=='water':
delalerts[author_id]['water']
# Check if the alerts[author_id] dictionary is empty and remove it
ifnotalerts[author_id]:
delalerts[author_id]
withopen(ALERTS_FILE, 'wb') asf:
pickle.dump(alerts, f)
awaitasyncio.sleep(60)
bot.run('TOKEN')