summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorschneefux <schneefux+commit@schneefux.xyz>2016-11-30 20:16:00 +0100
committerschneefux <schneefux+commit@schneefux.xyz>2016-11-30 20:16:00 +0100
commit30abc1e3489e652fdf0483d12040dabda69c8d72 (patch)
tree8d3fe219d70e8f61ddd198979352b37a3b728352
downloadVainbot-30abc1e3489e652fdf0483d12040dabda69c8d72.tar.gz
Vainbot-30abc1e3489e652fdf0483d12040dabda69c8d72.zip
add play requests
-rw-r--r--main.py114
1 files changed, 114 insertions, 0 deletions
diff --git a/main.py b/main.py
new file mode 100644
index 0000000..7a7797d
--- /dev/null
+++ b/main.py
@@ -0,0 +1,114 @@
+#!/usr/bin/env python3
+import pymongo
+import discord
+import asyncio
+import threading
+
+import parsedatetime
+import datetime
+
+
+TOKEN = "MjM5NzI3NzQ2Nzk0NzgyNzIw.Cu5BbA.z7bkO3Ncd_L80htVzREHYDsEGNk"
+VERSION = "v0.1.0"
+
+client = pymongo.MongoClient()
+db = client.vainbot
+
+cli = discord.Client()
+
+@cli.event
+async def on_ready():
+ print("Logged in as")
+ print(cli.user.name)
+ print(cli.user.id)
+ print("-----")
+
+def cleanup():
+ print("purging old data from database")
+ now = datetime.datetime.utcnow()
+ db.usercache.remove({
+ "date": {"$lt": now - datetime.timedelta(minutes=30)}
+ })
+ db.playwithme.find({
+ "end": {"$lt": now}
+ })
+
+async def get_user(id):
+ now = datetime.datetime.utcnow()
+ user = db.usercache.find_one({
+ "id": id,
+ "date": {"$gt": now - datetime.timedelta(minutes=30)}
+ })
+ if user:
+ return user
+ user = await cli.get_user_info(id)
+ db.usercache.insert_one({
+ "id": id,
+ "mention": user.mention,
+ "date": now
+ })
+ return await get_user(id)
+
+async def search_teammates(who, query):
+ now = datetime.datetime.utcnow()
+ cal = parsedatetime.Calendar()
+ cal.inc(now)
+
+ isrange = " for " in query or " until " in query
+ if isrange:
+ delimit = query.rfind("for")
+ if delimit == -1:
+ delimit = query.rfind("until")
+
+ startstr = query[:delimit]
+ endstr = query[delimit:]
+ else:
+ startstr = query
+ endstr = "for 30 minutes"
+
+ start, end, calnow = [cal.parseDT(d)[0] for d in (startstr, endstr, "now")]
+
+ db.playwithme.insert_one({
+ "player": who,
+ "start": start,
+ "end": end
+ })
+
+ print(who + " requested a game " + ", ".join([str(d) for d in (start, end)]))
+
+ if start == calnow:
+ players = []
+ for teammate in db.playwithme.find({"start": {"$lt": now}, "end": {"$gt": now}}):
+ mention = (await get_user(teammate["player"]))["mention"]
+ if not mention in players:
+ players += [mention]
+ if len(players) > 1:
+ return ", ".join(players) + " can play together."
+ return "Nobody can play right now. I will notify you."
+
+@cli.event
+async def on_message(message):
+ msg = message.content.lower()
+ if msg.startswith("?ping"):
+ await cli.send_message(message.channel, "Pong!")
+
+ if msg.startswith("?about"):
+ await cli.send_message(message.channel, "Vainglory Discord bot " + VERSION + " written & hosted by shutterfly")
+
+ if msg.startswith("?help"):
+ await cli.send_message(message.channel, "Available commands: ?help, ?ping, ?about")
+ await cli.send_message(message.channel, "Write <play with me [now (default) | in (time) | at,from (time)] [for (time, default 30 minutes) | until (time)]> to find teammates.")
+ await cli.send_message(message.channel, "Cancel your search with <don't play with me>.")
+
+ if msg.startswith("play with me"):
+ player = message.author.id
+ when = message.content[len("play with me"):]
+ response = await search_teammates(player, when)
+ await cli.send_message(message.channel, response)
+
+ if msg.startswith("don't play with me"):
+ db.playwithme.remove({"player": message.author.id})
+ await cli.send_message(message.channel, "Canceled all notifications.")
+
+threading.Timer(10 * 60, cleanup).start()
+cli.run(TOKEN)