Skip to content

Commit

Permalink
[CustomRoles] add role creation views
Browse files Browse the repository at this point in the history
  • Loading branch information
noahkw committed Jan 30, 2024
1 parent bc0313a commit 3687f53
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 1 deletion.
43 changes: 42 additions & 1 deletion cogs/CustomRoles.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,63 @@
import logging

import discord
from discord.ext import tasks
from discord import Embed
from discord.ext import tasks, commands

import db
from cogs import AinitMixin, CustomCog

from models import CustomRole
from views import RoleCreatorView

logger = logging.getLogger(__name__)


class NotBoosting(commands.CheckFailure):
def __init__(self):
super().__init__("You are not a server booster.")


def is_booster():
def predicate(ctx: commands.Context):
if (
ctx.author.premium_since is None
and not ctx.author.guild_permissions.administrator
):
raise NotBoosting

return True

return commands.check(predicate)


async def setup(bot):
await bot.add_cog(CustomRoles(bot))


class CustomRoles(CustomCog, AinitMixin):
def __index__(self, bot):
super().__init__(bot)

CustomRole.inject_bot(bot)

@commands.group(aliases=["customrole", "cr"], brief="Custom roles")
async def custom_roles(self, ctx: commands.Context):
if not ctx.invoked_subcommand:
await ctx.send_help(self.custom_roles)

@custom_roles.command(brief="Creates a custom role for you")
@is_booster()
async def create(self, ctx: commands.Context):
embed = Embed(
title="Create custom role",
description="Click below to create your own role.",
)
embed.set_author(name=ctx.guild.name, icon_url=ctx.guild.icon.url)

view = RoleCreatorView()
await ctx.send(view=view, embed=embed)

async def _delete_custom_role(self, session, custom_role: CustomRole) -> None:
try:
await custom_role.role.delete(
Expand Down
1 change: 1 addition & 0 deletions config.yml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ enabled_cogs:
- 'cogs.Logging'
- 'cogs.Main'
- 'cogs.Live'
- 'cogs.CustomRoles'
cogs:
biasoftheweek:
winner_role_name: 'BotW-Winner'
Expand Down
4 changes: 4 additions & 0 deletions views/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .role_creator import RoleCreatorView


__all__ = ("RoleCreatorView",)
75 changes: 75 additions & 0 deletions views/role_creator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import discord
from discord import Interaction, Button
from discord.ui import View, TextInput, Modal


class RoleCreatorView(View):
def __init__(self):
super().__init__()

@discord.ui.button(label="Click me to start", style=discord.ButtonStyle.blurple)
async def create_role(self, interaction: Interaction, button: Button):
await interaction.response.send_modal(RoleCreatorNameModal())


class RoleCreatorNameConfirmationView(View):
def __init__(self):
super().__init__()

@discord.ui.button(label="I confirm", style=discord.ButtonStyle.blurple)
async def confirm(self, interaction: Interaction, button: Button):
await interaction.response.send_modal(RoleCreatorColorModal())

@discord.ui.button(label="Choose another name", style=discord.ButtonStyle.red)
async def retry(self, interaction: Interaction, button: Button):
await interaction.response.send_modal(RoleCreatorNameModal())


class RoleCreatorNameModal(Modal, title="Choose your role's name"):
name = TextInput(
label="Role name",
placeholder="Your custom role name here...",
required=True,
min_length=3,
max_length=20,
)

async def on_submit(self, interaction: Interaction) -> None:
await interaction.response.send_message(
"Does your role name abide by the server rules? "
"If so, continue to role color selection.",
view=RoleCreatorNameConfirmationView(),
ephemeral=True,
)


class RoleCreatorColorConfirmationView(View):
def __init__(self):
super().__init__()

@discord.ui.button(label="I confirm.", style=discord.ButtonStyle.blurple)
async def confirm(self, interaction: Interaction, button: Button):
await interaction.response.send_message(
"Your role has been created! <a:winterletsgo:1079552519971278959>",
ephemeral=True,
)

@discord.ui.button(label="Choose another color", style=discord.ButtonStyle.red)
async def retry(self, interaction: Interaction, button: Button):
await interaction.response.send_modal(RoleCreatorColorModal())


class RoleCreatorColorModal(Modal, title="Choose your role's color"):
color = TextInput(
label="Role color",
placeholder="000000",
min_length=6,
max_length=6,
)

async def on_submit(self, interaction: Interaction) -> None:
await interaction.response.send_message(
"Is your role color legible?",
view=RoleCreatorColorConfirmationView(),
ephemeral=True,
)

0 comments on commit 3687f53

Please sign in to comment.