Skip to content

Instantly share code, notes, and snippets.

@mtimkovich
Last active August 14, 2025 07:20
Show Gist options
  • Select an option

  • Save mtimkovich/07dbd09ffade4a9fc43ed0b8b67aeb10 to your computer and use it in GitHub Desktop.

Select an option

Save mtimkovich/07dbd09ffade4a9fc43ed0b8b67aeb10 to your computer and use it in GitHub Desktop.
Dicebot
#!/usr/bin/env python3
import discord
from discord.ext import commands
from dotenv import dotenv_values
import logging
from random import randint
import re
from typing import Optional
config = dotenv_values()
discord.utils.setup_logging()
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='/', intents=intents)
@bot.command()
async def roll(ctx, cmd=''):
output = roll_fn(cmd)
if output is None:
return
async with ctx.typing():
await ctx.send(output)
def roll_fn(cmd: str) -> Optional[str]:
match = re.match(r'(\d+)?d(\d+)$', cmd)
if match is None:
return None
num_dice = int(match.group(1) or 1)
sides = int(match.group(2))
if num_dice == 0 or sides == 0:
return None
rolls = [str(randint(1, sides)) for _ in range(num_dice)]
output = ', '.join(rolls)
if len(output) > 2000:
return 'No.'
return output
bot.run(config['TOKEN'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment