Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions mantra/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
17 changes: 17 additions & 0 deletions mantra/core/plugins/Utilities/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import hikari
import lightbulb


async def create_tickets_channel(ctx: lightbulb.Context) -> hikari.GuildChannel:
channel = await ctx.get_guild().create_text_channel(
"tickets",
permission_overwrites=[
hikari.PermissionOverwrite(
id=ctx.guild_id,
type=0,
deny=hikari.Permissions.SEND_MESSAGES,
)
],
)

return channel
89 changes: 89 additions & 0 deletions mantra/core/plugins/Utilities/tickets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import logging

import hikari
import lightbulb
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")


@tickets.command
@lightbulb.command("ticket", "Command group containing ticket commands")
@lightbulb.implements(lightbulb.SlashCommandGroup)
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("startup", "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:
channel = await create_tickets_channel(ctx)

await TicketConfig.create(
guild_id=ctx.guild_id,
message=message,
channel=channel.id,
)
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()

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(
description=f"{ Emojis.SUCCESS} Tickets channel has been created successfully!",
color=Colors.SUCCESS,
),
flags=hikari.MessageFlag.EPHEMERAL,
)


@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)


def unload(bot: lightbulb.BotApp) -> None:
bot.remove_plugin(tickets)
10 changes: 10 additions & 0 deletions mantra/core/utils/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, "new_ticket")
.set_label("Create Ticket")
.set_emoji("📩")
.add_to_container()
)