summaryrefslogtreecommitdiff
path: root/keith.py
blob: 546a8b814a7d11a1ddc8c413eebab8550b8d5466 (plain)
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

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())