From d257dd6402141fcffceb8eee165b9a41b1f52934 Mon Sep 17 00:00:00 2001 From: Nishant Sapkota Date: Wed, 26 Oct 2022 21:31:22 +0545 Subject: [PATCH 1/3] feat: started working on tickets --- mantra/core/models.py | 16 ++++++ mantra/core/plugins/Utilities/__init__.py | 0 mantra/core/plugins/Utilities/tickets.py | 67 +++++++++++++++++++++++ mantra/core/utils/buttons.py | 10 ++++ 4 files changed, 93 insertions(+) create mode 100644 mantra/core/plugins/Utilities/__init__.py create mode 100644 mantra/core/plugins/Utilities/tickets.py diff --git a/mantra/core/models.py b/mantra/core/models.py index fe7bfde..9c087ef 100644 --- a/mantra/core/models.py +++ b/mantra/core/models.py @@ -26,3 +26,19 @@ class Meta: table = "guilds" table_description = "Stores information about the guild." + + +class TicketConfig(LogModel): + id = fields.IntField(pk=True, description="ID of the ticket config") + guild = fields.ForeignKeyField("main.Guild", related_name="ticket") + channel = fields.BigIntField(description="ID of the Ticket Channel") + message = fields.CharField( + max_length=255, description="Custom message to be sent", blank=True, null=True + ) + + class Meta: + table = "ticket_config" + table_description = ( + "This table stores all the information related to Ticket configuration" + ) + unique = "guild" diff --git a/mantra/core/plugins/Utilities/__init__.py b/mantra/core/plugins/Utilities/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mantra/core/plugins/Utilities/tickets.py b/mantra/core/plugins/Utilities/tickets.py new file mode 100644 index 0000000..446f4f4 --- /dev/null +++ b/mantra/core/plugins/Utilities/tickets.py @@ -0,0 +1,67 @@ +from math import perm + +import hikari +import lightbulb +import miru + +from mantra.core.models import TicketConfig +from mantra.core.utils.buttons import create_ticket_button +from mantra.core.utils.colors import Colors + +tickets = lightbulb.Plugin("Tickets", "Plugin that handles ticket commands") + + +@tickets.command +@lightbulb.command("ticket", "Command group containing ticket commands") +@lightbulb.implements(lightbulb.SlashCommandGroup) +async def ticket_command(_: lightbulb.Context) -> None: + ... + + +@ticket_command.child +@lightbulb.option("message", "Custom message to include in the embed", required=False) +@lightbulb.command("create", "Create a ticket embed in a channel", pass_options=True) +@lightbulb.implements(lightbulb.SlashSubCommand) +async def create_ticket( + ctx: lightbulb.Context, + message: str | None, +) -> None: + ticket_config = await TicketConfig.get_or_none(guild_id=ctx.guild_id) + if not ticket_config: + category = await ctx.get_guild().create_category("Tickets") + channel = await ctx.get_guild().create_text_channel( + "tickets", + category=category, + permission_overwrites=[ + hikari.PermissionOverwrite( + id=ctx.guild_id, + type=0, + deny=hikari.Permissions.SEND_MESSAGES, + ) + ], + ) + + await TicketConfig.create( + guild_id=ctx.guild_id, + message=message, + channel=channel.id, + ) + else: + channel = ctx.get_guild().get_channel(ticket_config.channel) + + await channel.send( + embed=hikari.Embed( + title="Ticket", + description=message or "Click the button below to create the Ticket!", + color=Colors.GENERIC, + ), + components=[create_ticket_button(ctx)], + ) + + +def load(bot: lightbulb.BotApp) -> None: + bot.add_plugin(tickets) + + +def unload(bot: lightbulb.BotApp) -> None: + bot.remove_plugin(tickets) diff --git a/mantra/core/utils/buttons.py b/mantra/core/utils/buttons.py index b6c1b3c..d24dec7 100644 --- a/mantra/core/utils/buttons.py +++ b/mantra/core/utils/buttons.py @@ -11,3 +11,13 @@ def create_source_button(ctx: lightbulb.Context, source: str) -> ActionRowBuilde .set_emoji("🔗") .add_to_container() ) + + +def create_ticket_button(ctx: lightbulb.Context) -> ActionRowBuilder: + return ( + ctx.app.rest.build_action_row() + .add_button(hikari.ButtonStyle.PRIMARY, "ticket") + .set_label("Create Ticket") + .set_emoji("📩") + .add_to_container() + ) From 087948ce80f5c6e0b1987f218ef734043525f834 Mon Sep 17 00:00:00 2001 From: Nishant Sapkota Date: Fri, 28 Oct 2022 21:38:22 +0545 Subject: [PATCH 2/3] feat: `ticket startup` command working now --- mantra/core/plugins/Utilities/__init__.py | 15 ++++++++++ mantra/core/plugins/Utilities/tickets.py | 35 ++++++++++++----------- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/mantra/core/plugins/Utilities/__init__.py b/mantra/core/plugins/Utilities/__init__.py index e69de29..a82b282 100644 --- a/mantra/core/plugins/Utilities/__init__.py +++ b/mantra/core/plugins/Utilities/__init__.py @@ -0,0 +1,15 @@ +import hikari +import lightbulb + + +async def create_tickets_channel(ctx: lightbulb.Context) -> hikari.GuildChannel: + return await ctx.get_guild().create_text_channel( + "tickets", + permission_overwrites=[ + hikari.PermissionOverwrite( + id=ctx.guild_id, + type=0, + deny=hikari.Permissions.SEND_MESSAGES, + ) + ], + ) diff --git a/mantra/core/plugins/Utilities/tickets.py b/mantra/core/plugins/Utilities/tickets.py index 446f4f4..d43d207 100644 --- a/mantra/core/plugins/Utilities/tickets.py +++ b/mantra/core/plugins/Utilities/tickets.py @@ -1,12 +1,13 @@ -from math import perm - import hikari import lightbulb -import miru +from tortoise.transactions import atomic from mantra.core.models import TicketConfig from mantra.core.utils.buttons import create_ticket_button from mantra.core.utils.colors import Colors +from mantra.core.utils.emojis import Emojis + +from . import create_tickets_channel tickets = lightbulb.Plugin("Tickets", "Plugin that handles ticket commands") @@ -18,9 +19,10 @@ async def ticket_command(_: lightbulb.Context) -> None: ... +@atomic @ticket_command.child @lightbulb.option("message", "Custom message to include in the embed", required=False) -@lightbulb.command("create", "Create a ticket embed in a channel", pass_options=True) +@lightbulb.command("startup", "Create a ticket embed in a channel", pass_options=True) @lightbulb.implements(lightbulb.SlashSubCommand) async def create_ticket( ctx: lightbulb.Context, @@ -28,18 +30,7 @@ async def create_ticket( ) -> None: ticket_config = await TicketConfig.get_or_none(guild_id=ctx.guild_id) if not ticket_config: - category = await ctx.get_guild().create_category("Tickets") - channel = await ctx.get_guild().create_text_channel( - "tickets", - category=category, - permission_overwrites=[ - hikari.PermissionOverwrite( - id=ctx.guild_id, - type=0, - deny=hikari.Permissions.SEND_MESSAGES, - ) - ], - ) + channel = await create_tickets_channel(ctx) await TicketConfig.create( guild_id=ctx.guild_id, @@ -48,6 +39,10 @@ async def create_ticket( ) else: channel = ctx.get_guild().get_channel(ticket_config.channel) + if not channel: + channel = await create_tickets_channel(ctx) + ticket_config.channel = channel.id + await ticket_config.save() await channel.send( embed=hikari.Embed( @@ -58,6 +53,14 @@ async def create_ticket( components=[create_ticket_button(ctx)], ) + await ctx.respond( + embed=hikari.Embed( + description=f"{ Emojis.SUCCESS} Tickets channel has been created successfully!", + color=Colors.SUCCESS, + ), + flags=hikari.MessageFlag.EPHEMERAL, + ) + def load(bot: lightbulb.BotApp) -> None: bot.add_plugin(tickets) From 577208a1ce77f25c173f9e515c28db36671f43af Mon Sep 17 00:00:00 2001 From: Nishant Sapkota Date: Sat, 29 Oct 2022 10:00:10 +0545 Subject: [PATCH 3/3] chore: minor fixes to the code --- mantra/core/plugins/Utilities/__init__.py | 4 ++- mantra/core/plugins/Utilities/tickets.py | 35 +++++++++++++++++------ mantra/core/utils/buttons.py | 2 +- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/mantra/core/plugins/Utilities/__init__.py b/mantra/core/plugins/Utilities/__init__.py index a82b282..d02bead 100644 --- a/mantra/core/plugins/Utilities/__init__.py +++ b/mantra/core/plugins/Utilities/__init__.py @@ -3,7 +3,7 @@ async def create_tickets_channel(ctx: lightbulb.Context) -> hikari.GuildChannel: - return await ctx.get_guild().create_text_channel( + channel = await ctx.get_guild().create_text_channel( "tickets", permission_overwrites=[ hikari.PermissionOverwrite( @@ -13,3 +13,5 @@ async def create_tickets_channel(ctx: lightbulb.Context) -> hikari.GuildChannel: ) ], ) + + return channel diff --git a/mantra/core/plugins/Utilities/tickets.py b/mantra/core/plugins/Utilities/tickets.py index d43d207..4aec05a 100644 --- a/mantra/core/plugins/Utilities/tickets.py +++ b/mantra/core/plugins/Utilities/tickets.py @@ -1,3 +1,5 @@ +import logging + import hikari import lightbulb from tortoise.transactions import atomic @@ -44,14 +46,16 @@ async def create_ticket( ticket_config.channel = channel.id await ticket_config.save() - await channel.send( - embed=hikari.Embed( - title="Ticket", - description=message or "Click the button below to create the Ticket!", - color=Colors.GENERIC, - ), - components=[create_ticket_button(ctx)], - ) + last_message = await channel.fetch_history().limit(1) + if not last_message: + await channel.send( + embed=hikari.Embed( + title="Ticket", + description=message or "Click the button below to create the Ticket!", + color=Colors.GENERIC, + ), + components=[create_ticket_button(ctx)], + ) await ctx.respond( embed=hikari.Embed( @@ -62,6 +66,21 @@ async def create_ticket( ) +@tickets.listener(hikari.InteractionCreateEvent) +async def handle_create_ticket(event: hikari.InteractionCreateEvent) -> None: + if ( + not isinstance(event.interaction, hikari.ComponentInteraction) + or event.interaction.custom_id != "new_ticket" + ): + return + + await event.interaction.create_initial_response( + hikari.ResponseType.MESSAGE_CREATE, + "Button clicked!", + flags=hikari.MessageFlag.EPHEMERAL, + ) + + def load(bot: lightbulb.BotApp) -> None: bot.add_plugin(tickets) diff --git a/mantra/core/utils/buttons.py b/mantra/core/utils/buttons.py index d24dec7..969ef8e 100644 --- a/mantra/core/utils/buttons.py +++ b/mantra/core/utils/buttons.py @@ -16,7 +16,7 @@ def create_source_button(ctx: lightbulb.Context, source: str) -> ActionRowBuilde def create_ticket_button(ctx: lightbulb.Context) -> ActionRowBuilder: return ( ctx.app.rest.build_action_row() - .add_button(hikari.ButtonStyle.PRIMARY, "ticket") + .add_button(hikari.ButtonStyle.PRIMARY, "new_ticket") .set_label("Create Ticket") .set_emoji("📩") .add_to_container()