Skip to content

Commit

Permalink
Add roll command
Browse files Browse the repository at this point in the history
  • Loading branch information
MycroftKang committed Jan 19, 2024
1 parent 295e066 commit be10ad9
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 1 deletion.
4 changes: 4 additions & 0 deletions changelogs/unreleased/add-roll-command.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Add roll command
pull_request:
type: features
Binary file added resources/app/bot/Game Die.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/app/bot/Keycap Digit 1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/app/bot/Keycap Digit 2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/app/bot/Keycap Digit 3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/app/bot/Keycap Digit 4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/app/bot/Keycap Digit 5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/app/bot/Keycap Digit 6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions src/bot/core/controllers/discord/roll.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import asyncio
import random

import discord
from discord.ext import commands

from mgylabs.i18n import __
from mgylabs.utils.config import resource_path
from mgylabs.utils.LogEntry import DiscordEventLogEntry

from .utils import Emoji
from .utils.feature import Feature
from .utils.MGCert import Level, MGCertificate
from .utils.MsgFormat import MsgFormatter


@commands.hybrid_command()
@MGCertificate.verify(level=Level.TRUSTED_USERS)
@Feature.Experiment()
async def roll(ctx: commands.Context):
"""
Roll a dice.
"""
embed = MsgFormatter.get(ctx, description=f"{Emoji.typing} {__('Rolling...')}")
dice_file = discord.File(resource_path("app/bot/Game Die.png"), filename="dice.png")
embed.set_author(name="Dice", icon_url="attachment://dice.png")

msg: discord.Message = await ctx.send(embed=embed, file=dice_file)

result = random.randint(1, 6)

DiscordEventLogEntry.Add(ctx, "DiceResult", {"result": result})

dice_file = discord.File(resource_path("app/bot/Game Die.png"), filename="dice.png")

number_file_name = f"Keycap Digit {result}.png"
discord_number_file_name = number_file_name.replace(" ", "")
number_file = discord.File(
resource_path(f"app/bot/{number_file_name}"),
filename=discord_number_file_name,
)

embed.description = None
embed.set_thumbnail(url=f"attachment://{discord_number_file_name}")

await asyncio.sleep(1)

await msg.edit(embed=embed, attachments=[dice_file, number_file])


async def setup(bot: commands.Bot):
bot.add_command(roll)
2 changes: 1 addition & 1 deletion src/bot/core/controllers/discord/utils/MsgFormat.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def set_avatar_url(avatar_url):
@staticmethod
def get(
ctx_or_iaction,
title,
title=None,
description=None,
fields: list = [],
show_req_user=True,
Expand Down
1 change: 1 addition & 0 deletions src/bot/discord_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# "core.controllers.discord.tts",
"core.controllers.discord.poll",
"core.controllers.discord.roulette",
"core.controllers.discord.roll",
# "core.controllers.discord.translate",
"core.controllers.discord.music",
"core.controllers.discord.tic_tac_toe",
Expand Down
9 changes: 9 additions & 0 deletions src/lib/mgylabs/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ def is_development_mode(debug_mode=True):
return not getattr(sys, "frozen", False) or debug_mode and ("--debug") in sys.argv


def resource_path(relative_path):
if is_development_mode():
base_path = "../../resources"
else:
base_path = "../resources"

return os.path.join(base_path, relative_path)


if is_development_mode():
CONFIG_PATH = "../data/config.json"
MGCERT_PATH = "../data/mgcert.json"
Expand Down

0 comments on commit be10ad9

Please sign in to comment.