summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--keith.py48
2 files changed, 50 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..855ec1e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+*.py[cod]
+venv/
diff --git a/keith.py b/keith.py
new file mode 100644
index 0000000..546a8b8
--- /dev/null
+++ b/keith.py
@@ -0,0 +1,48 @@
+
+import discord
+
+import random
+import re
+diceroll = re.compile(r"\b((\d+)?d(\d+))\b")
+client = discord.Client()
+
+@client.event
+async def on_ready():
+ print("Logged in")
+
+@client.event
+async def on_message(msg):
+ if msg.author == client.user:
+ return
+ matches = diceroll.findall(msg.content)
+ if matches:
+ resp = []
+ for match in matches:
+ full, count, sides = match
+ count = 1 if count == '' else int(count)
+ sides = int(sides)
+ if sides == 0:
+ return
+ if count > 999 or sides > 9999:
+ resp.append("`{}`: numbers are too big for me :(".format(full))
+ else:
+ result = 0
+ rolls = []
+ for i in range(count):
+ a = random.randint(1, sides)
+ result += a
+ if a == sides:
+ rolls.append("__"+str(a)+"__")
+ else:
+ rolls.append(str(a))
+ resp.append( "`{}`: `{}`: **{}**".format(full, ", ".join(rolls), result) )
+
+ await msg.channel.send("\n".join(resp))
+
+if __name__ == "__main__":
+ from os import getenv
+ tok = getenv("DICEBOT_TOKEN", None)
+ if tok is None:
+ print("Set the envvar DICEBOT_TOKEN to your token")
+ else:
+ client.run(tok.strip())