implement $roll command

This commit is contained in:
Denis-Cosmin Nutiu 2024-01-22 21:36:59 +02:00
parent b938bbc825
commit 0ecf7c738a
12 changed files with 67 additions and 2 deletions

2
config.yaml Normal file
View file

@ -0,0 +1,2 @@
discord:
token: <your discord token>

BIN
docs/bot_dice_roll.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

View file

@ -12,4 +12,8 @@ It features:
- ❌ Telemetry (We don't do that here) - ❌ Telemetry (We don't do that here)
- ❌ Paid Content. - ❌ Paid Content.
### Screenshots
![./docs/bot_dice_roll.png](./docs/bot_dice_roll.png)
Made with 💞 by [nuculabs.dev](https://blog.nuculabs.dev) Made with 💞 by [nuculabs.dev](https://blog.nuculabs.dev)

0
src/bot/__init__.py Normal file
View file

View file

22
src/bot/discord/bot.py Normal file
View file

@ -0,0 +1,22 @@
import logging
import disnake
from disnake.ext.commands import bot
from src.bot.discord.commands.dice import DiceCog
class NucuBot(bot.Bot):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._logger = logging.getLogger(__name__)
@staticmethod
def create() -> "NucuBot":
intents = disnake.Intents.all()
discord_bot = NucuBot(intents=intents, command_prefix="$")
discord_bot.add_cog(DiceCog(discord_bot))
return discord_bot
async def on_ready(self):
self._logger.info(f"Logged on as {self.user}!")

View file

View file

@ -0,0 +1,26 @@
from disnake.ext import commands
from src.dice.dice import DiceRoller
class DiceCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command(name="roll", aliases=["r"])
async def roll(self, ctx, dice_expression: str):
"""
A die can be rolled using the following expression:
- 1d20 will roll a 20-faceted die and output the result a random number between 1 and 20.
- 1d100 will roll a 100 faceted die.
- 2d20 will roll a two d20 dies and multiply the result by two.
- 2d20+5 will roll a two d20 dies and multiply the result by two and ads 5.
"""
if dice_expression == "0/0": # easter eggs
return await ctx.send("What do you expect me to do, destroy the universe?")
try:
roll_result = DiceRoller.roll(dice_expression)
await ctx.send(f"You rolled: {roll_result}")
except ValueError as e:
await ctx.send(f"Roll failed: {e}")

0
src/config/__init__.py Normal file
View file

View file

@ -14,7 +14,7 @@ class DiceRoller:
- 2d20+5 will roll a two d20 dies and multiply the result by two and ads 5. - 2d20+5 will roll a two d20 dies and multiply the result by two and ads 5.
""" """
_parser = DieParser() _parser = DieParser.create()
@staticmethod @staticmethod
def roll(expression: str, *, advantage: typing.Optional[bool] = None) -> int: def roll(expression: str, *, advantage: typing.Optional[bool] = None) -> int:

View file

@ -31,6 +31,10 @@ class DieParser:
self._semantics = DieSemantics() self._semantics = DieSemantics()
self._logger = logging.getLogger("DieParser") self._logger = logging.getLogger("DieParser")
@staticmethod
def create() -> "DieParser":
return DieParser()
def parse(self, expression: str) -> int: def parse(self, expression: str) -> int:
""" """
Parses the die expression and returns the result. Parses the die expression and returns the result.

View file

@ -1,6 +1,13 @@
import logging
from src.bot.discord.bot import NucuBot
def main(): def main():
print("hello world") bot = NucuBot.create()
bot.run("<token here>")
if __name__ == "__main__": if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
main() main()