Skip to content

Commit

Permalink
rules.py: use modal for rule add
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenChen committed Nov 9, 2024
1 parent 90557aa commit d3587a2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
17 changes: 4 additions & 13 deletions cogs/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import TYPE_CHECKING
from utils.checks import is_staff, is_staff_app
from utils.database.configuration import Rule
from utils.modals import RuleAddition
from utils.utils import KurisuCooldown, text_to_discord_file, simple_embed, gen_color
from utils.views import BasePaginator, PaginatedEmbedView

Expand Down Expand Up @@ -153,19 +154,9 @@ async def updaterules(self, ctx: KurisuContext):

@is_staff_app("OP")
@app_commands.command()
async def add_rule(self, interaction: discord.Interaction, number: int, title: str, description: str):
"""Adds or edits a current rule
Args
number: Number of the rule to add/edit.
title: Title of the rule.
description: Description of the rule.
"""
if self.configuration.rules.get(number):
await self.configuration.edit_rule(number, title, description)
await interaction.response.send_message(f"Rule {number} edited successfully!")
else:
await self.configuration.add_rule(number, title, description)
await interaction.response.send_message(f"Rule {number} added successfully!")
async def add_rule(self, interaction: discord.Interaction):
"""Adds or edits a current rule. Missing fields stay the same for editing rules."""
await interaction.response.send_modal(RuleAddition())

@is_staff_app("OP")
@app_commands.command()
Expand Down
40 changes: 40 additions & 0 deletions utils/modals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import discord

from discord import ui, Embed
from utils.utils import gen_color
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from kurisu import Kurisu


class RuleAddition(ui.Modal, title='Rule Addition'):
rule_number = ui.TextInput(label='Rule Number')
rule_title = ui.TextInput(label='Title', required=False)
rule_description = ui.TextInput(label='Description', style=discord.TextStyle.paragraph, required=False)

async def on_submit(self, interaction: discord.Interaction):
bot: 'Kurisu' = interaction.client # type: ignore
try:
number = int(self.rule_number.value)
except ValueError:
await interaction.response.send_message('Invalid rule number', ephemeral=True)
return

rule = bot.configuration.rules.get(number)
if rule:
title = self.rule_title.value if self.rule_title.value else rule.title
description = self.rule_description.value if self.rule_description.value else rule.description
await bot.configuration.edit_rule(number, title, description)
await interaction.response.send_message(f"Rule {number} edited successfully!", embed=Embed(title=title, description=description, color=gen_color(number)))
else:
title = self.rule_title.value
description = self.rule_description.value
if not title or description:
return await interaction.response.send_message("Title and description is required for new rules", ephemeral=True)
await bot.configuration.add_rule(number, title, description)
await interaction.response.send_message(f"Rule {number} created successfully!", embed=Embed(title=f"Rule {number} - {title}", description=description, color=gen_color(number)))
await bot.reload_extension("cogs.rules")

async def on_error(self, interaction: discord.Interaction, error: Exception) -> None:
await interaction.response.send_message('Error when creating rule', ephemeral=True)

0 comments on commit d3587a2

Please sign in to comment.