diff options
author | ubq323 <ubq323@ubq323.website> | 2021-12-24 17:35:33 +0000 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2021-12-24 17:35:33 +0000 |
commit | a584ef534104e38224b1254dbbb96ca236f9bcca (patch) | |
tree | fd028e0db66ec06c2b2a9f363f99ff190790a96d |
pisstrunk
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | keith.py | 48 |
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()) |