-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.py
392 lines (328 loc) · 11.2 KB
/
bot.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
from asyncio import TimeoutError
from datetime import datetime
from subprocess import check_output
from discord.ext import commands, tasks
from discord import Embed, Intents
from pymongo import MongoClient
from pymongo.errors import ConnectionFailure
from tasks import BotTasks
from helpers import adjacent_days, plist, Weekdays, Emojis
from mongo_tracker import Tracker
try:
import configparser
# Load config
bot_config = configparser.ConfigParser()
bot_config.read("config.ini")
token = bot_config["secrets"]["token"]
bot_prefix = bot_config["discord"]["botPrefix"]
db_host = bot_config["db"]["host"]
db_port = int(bot_config["db"]["port"])
db_password = bot_config["db"]["password"]
alert_time = int(bot_config["alerts"]["time"])
except KeyError:
# Fall back to environment variables
from os import environ
token = environ["token"]
bot_prefix = environ["botPrefix"]
db_host = environ["dbHost"]
db_port = int(environ["dbPort"])
db_password = environ["dbPassword"]
alert_time = int(environ["alertTime"])
# Bot init
intents = Intents.default()
intents.members = True
description = """A bot to assist with hearding players for D&D sessions."""
bot = commands.Bot(command_prefix=bot_prefix, description=description, intents=intents)
startTime = datetime.now().replace(microsecond=0)
dbh = MongoClient(host=db_host, port=db_port, password=db_password)
tracker = Tracker(dbh["dnd-bot"])
# Events
@bot.event
async def on_ready():
print(f"[{startTime}] - Logged in as {bot.user.name} - {bot.user.id}")
# Commands
@bot.command()
async def status(ctx):
try:
dbh.admin.command("ping")
except ConnectionFailure:
db_status = "offline"
else:
db_status = "online"
git = check_output(["git", "rev-parse", "--short", "HEAD"]).decode("ascii").strip()
now = datetime.now().replace(microsecond=0)
await ctx.message.channel.send(
f"Up for **{now - startTime}** on `{git}`. Database is **{db_status}**."
)
@bot.command()
async def config(ctx):
questions = ["session day", "first alert", "second alert"]
answers = [await ask_for_day(ctx, q) for q in questions]
def map_emoji_to_day_value(emoji):
for e in Emojis:
if e.value == emoji:
return Weekdays[e.name].value
mapped_answers = [map_emoji_to_day_value(a) for a in answers]
config = {
questions[i].replace(" ", "-", 1): mapped_answers[i]
for i in range(len(mapped_answers))
}
config["session-time"] = await ask_for_time(ctx)
tracker.create_guild_config(
guild_id=ctx.guild.id,
dm_user=ctx.author,
session_day=config["session-day"],
session_time=config["session-time"],
meeting_room=ctx.message.channel.id,
first_alert=config["first-alert"],
second_alert=config["second-alert"],
)
await ctx.message.channel.send("Config saved!")
async def ask_for_time(ctx):
my_message = await ctx.message.channel.send("Configure time (24h HH:MM):")
def check(m):
return ctx.author == m.author
try:
response = await bot.wait_for("message", timeout=30.0, check=check)
except TimeoutError:
await ctx.message.channel.send("Fail! React faster!")
to_return = None
else:
to_return = response.content.strip()
finally:
await my_message.delete()
return to_return
async def ask_for_day(ctx, ask):
my_message = await ctx.message.channel.send(f"Configure: {ask}")
for emoji in Emojis:
await my_message.add_reaction(emoji.value)
def check(reaction, user):
return user == ctx.author and any(e.value == str(reaction) for e in Emojis)
try:
reaction, _ = await bot.wait_for("reaction_add", timeout=10.0, check=check)
except TimeoutError:
await ctx.message.channel.send("Fail! React faster!")
to_return = None
else:
to_return = str(reaction)
finally:
await my_message.delete()
return to_return
@bot.command()
async def unconfig(ctx):
tracker.rm_guild_config(ctx.guild.id)
await ctx.message.add_reaction("👋")
@bot.command()
async def register(ctx):
tracker.register_player(ctx.guild.id, ctx.author)
await ctx.message.add_reaction("✅")
@bot.command()
async def players(ctx):
players = tracker.get_players_for_guild(ctx.guild.id)
await ctx.message.channel.send(
embed=Embed().from_dict(
{
"title": "Registered Players",
"fields": [
{"name": player["name"], "value": f"ID: {player['id']}"}
for player in players
],
}
)
)
@bot.command()
async def cmds(ctx):
await ctx.message.channel.send(
embed=Embed().from_dict(
{
"title": "Available Commands",
"fields": [
{"name": cmd.name, "value": f"`{cmd.name}`"} for cmd in bot.commands
],
}
)
)
@bot.command()
async def reset(ctx):
tracker.reset(ctx.guild.id)
await ctx.message.add_reaction("✅")
@bot.command()
async def skip(ctx):
tracker.skip(ctx.guild.id)
await ctx.message.channel.send("Skipping this week!")
@bot.command()
async def list(ctx):
accept, decline, dream, cancel = tracker.get_all(ctx.guild.id)
await ctx.message.channel.send(
embed=Embed().from_dict(
{
"title": "Lists",
"fields": [
{"name": "Accepted", "value": plist(accept)},
{"name": "Declined", "value": plist(decline)},
{"name": "Dreamers", "value": plist(dream)},
{"name": "Cancelled", "value": plist(cancel)},
],
}
)
)
# Support inv [add|remove]
@bot.group()
async def inv(ctx):
if ctx.invoked_subcommand is None:
if (
len(
(
inv := tracker.get_inventory_for_player(
ctx.guild.id, ctx.message.author
)
)
)
== 0
):
inv_message = "<< Empty >>"
else:
inv_message = "\n".join([f"{i['qty']}:{i['item']}" for i in inv])
await ctx.message.channel.send(
embed=Embed().from_dict(
{
"fields": [
{
"name": f"__*{ctx.message.author.name}'s Inventory:*__",
"value": inv_message,
}
]
}
)
)
@inv.command(name="add")
async def add(ctx):
_, _, items = ctx.message.content.split(" ", 2)
unpacked_items = items.split(", ")
for pair in unpacked_items:
qty, item = pair.split(":")
tracker.add_to_player_inventory(ctx.guild.id, ctx.author, item, qty)
await ctx.message.add_reaction("✅")
@inv.command(name="remove")
async def remove(ctx):
_, _, item = ctx.message.content.split(" ", 2)
tracker.rm_from_player_inventory(ctx.guild.id, ctx.author, item)
await ctx.message.add_reaction("✅")
@inv.command(name="update")
async def update(ctx):
_, _, items = ctx.message.content.split(" ", 2)
unpacked_items = items.split(", ")
for pair in unpacked_items:
qty, item = pair.split(":")
tracker.update_player_inventory(ctx.guild.id, ctx.author, item, qty)
await ctx.message.add_reaction("✅")
# Support rsvp [accept|decline]
@bot.group()
async def rsvp(ctx):
if ctx.invoked_subcommand is None:
await ctx.message.channel.send(
f"Please use either `{bot_prefix}rsvp accept` or `{bot_prefix}rsvp decline`."
)
@rsvp.command(name="accept")
async def _accept(ctx):
tracker.add_attendee_for_guild(ctx.guild.id, ctx.author)
await ctx.message.channel.send(
embed=Embed().from_dict(
{
"fields": [
{
"name": "Accepted",
"value": "Thanks for confirming!",
},
{
"name": "Attendees",
"value": plist(tracker.get_attendees_for_guild(ctx.guild.id)),
},
]
}
)
)
tracker.rm_decliner_for_guild(ctx.guild.id, ctx.author)
@rsvp.command(name="decline")
async def _decline(ctx):
tracker.add_decliner_for_guild(ctx.guild.id, ctx.author)
await ctx.message.channel.send(
embed=Embed().from_dict(
{
"fields": [
{"name": "Declined", "value": "No problem, see you next time!"},
{
"name": "Those that have declined",
"value": plist(tracker.get_decliners_for_guild(ctx.guild.id)),
},
]
}
)
)
tracker.rm_attendee_for_guild(ctx.guild.id, ctx.author)
# Support vote [dream|cancel]
@bot.group()
async def vote(ctx):
if ctx.invoked_subcommand is None:
await ctx.message.channel.send(
f"Please `{bot_prefix}vote dream` or `{bot_prefix}vote cancel`"
)
@vote.command(name="dream")
async def _dream(ctx):
tracker.add_dreamer_for_guild(ctx.guild.id, ctx.author)
await ctx.message.channel.send(
embed=Embed().from_dict(
{
"fields": [
{
"name": "Dreaming",
"value": "You've been added to the dreaming list!",
},
{
"name": "Other dreamers",
"value": plist(tracker.get_dreamers_for_guild(ctx.guild.id)),
},
]
}
)
)
@vote.command(name="cancel")
async def _cancel(ctx):
tracker.add_canceller_for_guild(ctx.guild.id, ctx.author)
await ctx.message.channel.send(
embed=Embed().from_dict(
{
"fields": [
{
"name": "Cancelling",
"value": "You've voted to cancel this week.",
},
{
"name": "Others that have cancelled",
"value": plist(tracker.get_cancellers_for_guild(ctx.guild.id)),
},
]
}
)
)
bt = BotTasks(bot)
@tasks.loop(hours=1)
async def alert_dispatcher():
await bot.wait_until_ready()
if int(datetime.now().strftime("%H")) != alert_time:
return
today = datetime.now().weekday()
day_before, _ = adjacent_days(today)
for config in tracker.get_first_alert_configs(today):
if not tracker.is_full_group(config["guild"]):
await bt.first_alert(config)
for config in tracker.get_second_alert_configs(today):
if not tracker.is_full_group(config["guild"]):
await bt.second_alert(config)
for config in tracker.get_session_day_configs(today):
await bt.send_dm(config, tracker)
for config in tracker.get_session_day_configs(day_before):
bt.reset(config, tracker)
if __name__ == "__main__":
alert_dispatcher.start()
bot.run(token)