absolute-destiny/bot.py
2021-06-23 14:43:59 -04:00

68 lines
1.4 KiB
Python

import discord
import random
import sys
client = discord.Client()
BOXED_NUMBERS = [
":zero:",
":one:",
":two:",
":three:",
":four:",
":five:",
":six:",
":seven:",
":eight:",
":nine:",
":keycap_ten:",
]
def boxed(n):
if 0 <= n and n < len(BOXED_NUMBERS):
return BOXED_NUMBERS[n]
else:
return "[{}]".format(n)
def succ(n):
if n <= 6:
return ':fire:'
elif 7 <= n and n <= 9:
return ':person_shrugging:'
else:
return ':tada:'
def roll(mod):
rolls = []
for i in range(2):
rolls.append(random.randint(1,6))
result = sum(rolls) + mod
message = '+'.join(boxed(r) for r in rolls)
message += '{:+}'.format(mod)
message += ' = **{}** {}'.format(result, succ(result))
return message
@client.event
async def on_ready():
print('logged in as {0.user}'.format(client), file=sys.stderr)
await client.change_presence(activity=discord.Game('!n for 2d6+n'))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!'):
try:
mod=int(message.content.split()[0][1:])
except ValueError:
print('error parsing message {0}'.format(message.content), file=sys.stderr)
return
await message.channel.send(roll(mod))
with open('token') as f:
token = f.read().strip()
client.run(token)