diff options
| author | schneefux <schneefux+commit@schneefux.xyz> | 2016-12-09 22:06:47 +0100 |
|---|---|---|
| committer | schneefux <schneefux+commit@schneefux.xyz> | 2016-12-09 22:06:47 +0100 |
| commit | 7727e92d5c95f28b91eb3ab5a00c519caa5648f7 (patch) | |
| tree | 0dda077068c538576f7f218a3e7e251c538d5766 | |
| parent | b1afb25a11344f72c8c574d8b48fe1a76520c8c4 (diff) | |
| download | Vainbot-7727e92d5c95f28b91eb3ab5a00c519caa5648f7.tar.gz Vainbot-7727e92d5c95f28b91eb3ab5a00c519caa5648f7.zip | |
rewrite search_teammates
| -rw-r--r-- | main.py | 88 |
1 files changed, 30 insertions, 58 deletions
@@ -21,15 +21,6 @@ cli = discord.Client() async def on_ready(): log.warning("Logged in as " + cli.user.name + " (" + str(cli.user.id) + ")") -def cleanup(): - log.warning("purging old data from database") - now = datetime.datetime.utcnow() - db.usercache.remove({ - "date": {"$lt": now - datetime.timedelta(minutes=30)} - }) - db.playwithme.remove({ - "end": {"$lt": now} - }) async def get_user(id): now = datetime.datetime.utcnow() @@ -47,8 +38,10 @@ async def get_user(id): }) return await get_user(id) -async def search_teammates(who, query, notify=True): + +async def search_teammates(server, channel, who, query, notify=True): cal = parsedatetime.Calendar() + now = cal.parseDT("now")[0] if " for " in query or " until " in query: delimit = query.rfind("for") @@ -65,46 +58,43 @@ async def search_teammates(who, query, notify=True): end = cal.parseDT(endstr, sourceTime=start)[0] if notify: - db.playwithme.insert_one({ + queueid = db.playwithme.insert_one({ "player": who.id, "start": start, "end": end - }) + }).inserted_id - log.info(who.name + " requested a game " + ", ".join([str(d) for d in (start, end)])) + playrole = [r for r in server.roles if r.name == "play"][0] + await cli.send_message(channel, who.mention + " You have been put into the queue.") + await asyncio.sleep((start-now).total_seconds()) + await cli.add_roles(who, playrole) + await cli.send_message(channel, "%s You were given the 'play' role and will be in queue for %s minutes." % (who.mention, int((end-start).total_seconds() // 60))) players = [] - query = { - "$or": [ - { - "start": { + query = { "$or": [ + { "start": { "$gte": start, - "$lte": end - } - }, - { - "end": { + "$lte": end } }, + { "end": { "$gte": start, - "$lte": end - } - }, - { - "start": {"$lte": start}, + "$lte": end } }, + { "start": {"$lte": start}, "end": {"$gte": end} - } - ] - } + } ] } for teammate in db.playwithme.find(query): mention = (await get_user(teammate["player"]))["mention"] - if not mention in players: + if not mention in players and mention != who.mention: players += [mention] - if len(players) > 1 or (len(players) == 1 and not notify): - return ", ".join(players) + " can play." + if len(players) > 0: + await cli.send_message(channel, "%s playing right now: %s" % (who.mention, ", ".join(players))) else: - if notify: - return "Nobody can play at that time. I will notify you if anyone wants to play between %s and %s. It is now %s." % (start.strftime("%H:%M"), end.strftime("%H:%M"), datetime.datetime.now().strftime("%H:%M")) - else: - return "Nobody can play at that time." + await cli.send_message(channel, "%s nobody is playing right now. I'll notify you when somebody can." % (who.mention)) + + if notify: + await asyncio.sleep((end-start).total_seconds()) + await cli.remove_roles(who, playrole) + db.playwithme.remove({"_id": queueid}) + await cli.send_message(channel, "%s You have been removed from the queue and were taken the play role." % (who.mention)) @cli.event async def on_message(message): @@ -128,33 +118,15 @@ async def on_message(message): else: when = message.content[len("who can play"):] notify = False - response = await search_teammates(message.author, when, notify) - await cli.send_message(message.channel, response) + await search_teammates(message.server, message.channel, message.author, when, notify) if msg.startswith("don't play with me") or msg.startswith("dont play with me"): db.playwithme.remove({"player": message.author.id}) - await cli.send_message(message.channel, "Canceled all notifications.") - - # - # "play" role Discoid backwards compatibility - # - if msg.startswith("\\getrole play") or msg.startswith("\\play"): - # toggles the "play" role - log.info("toggled " + message.author.name + "'s play role") - for role in message.author.roles: - if role.name == "play": - await cli.remove_roles(message.author, role) - await cli.send_message(message.channel, "Removed the 'play' role.") - return - - for role in message.server.roles: - if role.name == "play": - await cli.add_roles(message.author, role) - await cli.send_message(message.channel, "Added the 'play' role.") + await cli.remove_roles(message.author, [r for r in message.server.roles if r.name == "play"][0]) + await cli.send_message(message.channel, "%s: Canceled all notifications and removed the 'play' role." % (message.author.mention)) if __name__ == '__main__': logging.basicConfig() log = logging.getLogger() log.setLevel(logging.INFO) - threading.Timer(10 * 60, cleanup).start() cli.run(TOKEN) |
