implement $roll command
This commit is contained in:
parent
b938bbc825
commit
0ecf7c738a
12 changed files with 67 additions and 2 deletions
2
config.yaml
Normal file
2
config.yaml
Normal file
|
@ -0,0 +1,2 @@
|
|||
discord:
|
||||
token: <your discord token>
|
BIN
docs/bot_dice_roll.png
Normal file
BIN
docs/bot_dice_roll.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 40 KiB |
|
@ -12,4 +12,8 @@ It features:
|
|||
- ❌ Telemetry (We don't do that here)
|
||||
- ❌ Paid Content.
|
||||
|
||||
### Screenshots
|
||||
|
||||
![./docs/bot_dice_roll.png](./docs/bot_dice_roll.png)
|
||||
|
||||
Made with 💞 by [nuculabs.dev](https://blog.nuculabs.dev)
|
||||
|
|
0
src/bot/__init__.py
Normal file
0
src/bot/__init__.py
Normal file
0
src/bot/discord/__init__.py
Normal file
0
src/bot/discord/__init__.py
Normal file
22
src/bot/discord/bot.py
Normal file
22
src/bot/discord/bot.py
Normal 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}!")
|
0
src/bot/discord/commands/__init__.py
Normal file
0
src/bot/discord/commands/__init__.py
Normal file
26
src/bot/discord/commands/dice.py
Normal file
26
src/bot/discord/commands/dice.py
Normal 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
0
src/config/__init__.py
Normal file
|
@ -14,7 +14,7 @@ class DiceRoller:
|
|||
- 2d20+5 will roll a two d20 dies and multiply the result by two and ads 5.
|
||||
"""
|
||||
|
||||
_parser = DieParser()
|
||||
_parser = DieParser.create()
|
||||
|
||||
@staticmethod
|
||||
def roll(expression: str, *, advantage: typing.Optional[bool] = None) -> int:
|
||||
|
|
|
@ -31,6 +31,10 @@ class DieParser:
|
|||
self._semantics = DieSemantics()
|
||||
self._logger = logging.getLogger("DieParser")
|
||||
|
||||
@staticmethod
|
||||
def create() -> "DieParser":
|
||||
return DieParser()
|
||||
|
||||
def parse(self, expression: str) -> int:
|
||||
"""
|
||||
Parses the die expression and returns the result.
|
||||
|
|
|
@ -1,6 +1,13 @@
|
|||
import logging
|
||||
|
||||
from src.bot.discord.bot import NucuBot
|
||||
|
||||
|
||||
def main():
|
||||
print("hello world")
|
||||
bot = NucuBot.create()
|
||||
bot.run("<token here>")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
main()
|
||||
|
|
Loading…
Reference in a new issue