mirror of
https://github.com/wlinator/luminara.git
synced 2024-10-02 18:23:12 +00:00
Add delete and list custom reaction command
This commit is contained in:
parent
4c2206a99e
commit
7f798c779a
5 changed files with 194 additions and 2 deletions
|
@ -8,6 +8,7 @@ resources = JsonCache.read_json("art")
|
|||
|
||||
check_icon = resources["icons"]["check"]
|
||||
cross_icon = resources["icons"]["cross"]
|
||||
exclaim_icon = resources["icons"]["exclaim"]
|
||||
logo = resources["logo"]["transparent"]
|
||||
|
||||
def create_embed(title: str, description: str, color: int, icon_url: str) -> discord.Embed:
|
||||
|
@ -46,4 +47,35 @@ def create_failure_embed(trigger_text: str, is_emoji: bool, limit_reached: bool
|
|||
else:
|
||||
description += "Failed to add custom reaction."
|
||||
|
||||
return create_embed("Custom Reaction Creation Failed", description, 0xFF0000, cross_icon)
|
||||
return create_embed("Custom Reaction Creation Failed", description, 0xFF4500, cross_icon)
|
||||
|
||||
|
||||
def create_deletion_embed(trigger_text: str, is_emoji: bool) -> discord.Embed:
|
||||
trigger_text = formatter.shorten(trigger_text, 50)
|
||||
|
||||
description = f"**Trigger Text:** `{trigger_text}`\n"
|
||||
description += "Custom reaction has been successfully deleted."
|
||||
|
||||
return create_embed("Custom Reaction Deleted", description, 0xFF8C00, check_icon)
|
||||
|
||||
def create_not_found_embed(reaction_id: int) -> discord.Embed:
|
||||
description = f"**Reaction ID:** `{reaction_id}`\n"
|
||||
description += "No custom reaction found with the provided ID."
|
||||
|
||||
return create_embed("Custom Reaction Not Found", description, 0xFF4500, cross_icon)
|
||||
|
||||
def create_no_triggers_embed() -> discord.Embed:
|
||||
description = (
|
||||
"There are no custom reactions set up yet.\n\n"
|
||||
"To create a new custom reaction, use the following commands:\n"
|
||||
"`/trigger add emoji` - Add a new custom emoji reaction.\n"
|
||||
"`/trigger add response` - Add a new custom text reaction.\n\n"
|
||||
"**Emoji Reaction:**\n"
|
||||
"An emoji reaction will react with a specific emoji when the trigger text is detected.\n\n"
|
||||
"**Text Reaction:**\n"
|
||||
"A text reaction will respond with a specific text message when the trigger text is detected."
|
||||
)
|
||||
|
||||
return create_embed("No Custom Reactions Found", description, 0xFF8C00, exclaim_icon)
|
||||
|
||||
|
||||
|
|
|
@ -4,6 +4,8 @@ import discord
|
|||
|
||||
from Client import LumiBot
|
||||
from modules.triggers.add import add_reaction
|
||||
from modules.triggers.delete import delete_reaction
|
||||
from modules.triggers.list import list_reactions
|
||||
|
||||
|
||||
class Triggers(commands.Cog):
|
||||
|
@ -44,6 +46,31 @@ class Triggers(commands.Cog):
|
|||
is_full_match: bool
|
||||
):
|
||||
await add_reaction(ctx, trigger_text, None, emoji.id, True, is_full_match)
|
||||
|
||||
@trigger.command(
|
||||
name="delete",
|
||||
description="Delete an existing custom reaction.",
|
||||
help="Delete an existing custom reaction from the database.",
|
||||
)
|
||||
@commands.guild_only()
|
||||
async def delete_reaction_command(
|
||||
self,
|
||||
ctx,
|
||||
reaction_id: int
|
||||
):
|
||||
await delete_reaction(ctx, reaction_id)
|
||||
|
||||
@trigger.command(
|
||||
name="list",
|
||||
description="List all custom reactions.",
|
||||
help="List all custom reactions for the current guild.",
|
||||
)
|
||||
@commands.guild_only()
|
||||
async def list_reactions_command(
|
||||
self,
|
||||
ctx
|
||||
):
|
||||
await list_reactions(ctx)
|
||||
|
||||
def setup(client: LumiBot):
|
||||
client.add_cog(Triggers(client))
|
30
modules/triggers/delete.py
Normal file
30
modules/triggers/delete.py
Normal file
|
@ -0,0 +1,30 @@
|
|||
from discord.ext import bridge
|
||||
from services.reactions_service import CustomReactionsService
|
||||
from lib.embeds.triggers import create_deletion_embed, create_failure_embed, create_not_found_embed
|
||||
|
||||
async def delete_reaction(ctx: bridge.Context, reaction_id: int) -> None:
|
||||
if ctx.guild is None:
|
||||
return
|
||||
|
||||
reaction_service = CustomReactionsService()
|
||||
guild_id: int = ctx.guild.id
|
||||
|
||||
# Check if the reaction exists and belongs to the guild
|
||||
reaction = await reaction_service.find_id(reaction_id)
|
||||
if reaction is None or reaction["guild_id"] != guild_id or reaction["is_global"]:
|
||||
embed = create_not_found_embed(reaction_id)
|
||||
await ctx.respond(embed=embed)
|
||||
return
|
||||
|
||||
trigger_text = reaction["trigger_text"]
|
||||
is_emoji = reaction["is_emoji"]
|
||||
|
||||
# Attempt to delete the reaction
|
||||
success: bool = await reaction_service.delete_custom_reaction(reaction_id)
|
||||
|
||||
if success:
|
||||
embed = create_deletion_embed(trigger_text, is_emoji)
|
||||
await ctx.respond(embed=embed)
|
||||
else:
|
||||
embed = create_failure_embed(trigger_text, is_emoji)
|
||||
await ctx.respond(embed=embed)
|
48
modules/triggers/list.py
Normal file
48
modules/triggers/list.py
Normal file
|
@ -0,0 +1,48 @@
|
|||
import discord
|
||||
from discord.ext import bridge, pages
|
||||
from services.reactions_service import CustomReactionsService
|
||||
from lib.embeds.triggers import create_no_triggers_embed
|
||||
from config.parser import JsonCache
|
||||
import datetime
|
||||
|
||||
resources = JsonCache.read_json("art")
|
||||
|
||||
check_icon = resources["icons"]["check"]
|
||||
logo = resources["logo"]["transparent"]
|
||||
|
||||
async def list_reactions(ctx: bridge.Context) -> None:
|
||||
if ctx.guild is None:
|
||||
return
|
||||
|
||||
reaction_service = CustomReactionsService()
|
||||
guild_id: int = ctx.guild.id
|
||||
|
||||
# Fetch all reactions for the guild
|
||||
reactions = await reaction_service.find_all_by_guild(guild_id)
|
||||
if not reactions:
|
||||
embed = create_no_triggers_embed()
|
||||
await ctx.respond(embed=embed)
|
||||
return
|
||||
|
||||
# Create pages for pagination
|
||||
pages_list = []
|
||||
for reaction in reactions:
|
||||
description = (
|
||||
f"**Trigger Text:** `{reaction['trigger_text']}`\n"
|
||||
f"**Reaction Type:** {'Emoji' if reaction['is_emoji'] else 'Text'}\n"
|
||||
f"{'**Emoji ID:** `{}`'.format(str(reaction['emoji_id'])) if reaction['is_emoji'] else '**Response:** `{}`'.format(reaction['response'])}\n"
|
||||
f"**Full Match:** `{'True' if reaction['is_full_match'] else 'False'}`\n"
|
||||
f"**Usage Count:** `{reaction['usage_count']}`"
|
||||
)
|
||||
embed = discord.Embed(
|
||||
title=f"ID: {reaction['id']}",
|
||||
description=description,
|
||||
color=0xFF8C00
|
||||
)
|
||||
embed.set_author(name="Custom Reactions", icon_url=check_icon)
|
||||
embed.set_footer(text="Reaction Service", icon_url=logo)
|
||||
embed.timestamp = datetime.datetime.utcnow()
|
||||
pages_list.append(embed)
|
||||
|
||||
paginator = pages.Paginator(pages=pages_list, timeout=180.0)
|
||||
await paginator.respond(ctx, ephemeral=False)
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Optional, Dict, Any
|
||||
from typing import Optional, Dict, Any, List
|
||||
from datetime import datetime
|
||||
from db import database
|
||||
|
||||
|
@ -40,6 +40,61 @@ class CustomReactionsService:
|
|||
}
|
||||
return None
|
||||
|
||||
async def find_id(
|
||||
self, reaction_id: int
|
||||
) -> Optional[Dict[str, Any]]:
|
||||
query = """
|
||||
SELECT * FROM custom_reactions
|
||||
WHERE id = ?
|
||||
LIMIT 1
|
||||
"""
|
||||
result = database.select_query(query, (reaction_id,))
|
||||
if result:
|
||||
reaction = result[0] # Get the first result from the list
|
||||
return {
|
||||
"id": reaction[0],
|
||||
"trigger_text": reaction[1],
|
||||
"response": reaction[2],
|
||||
"emoji_id": reaction[3],
|
||||
"is_emoji": reaction[4],
|
||||
"is_full_match": reaction[5],
|
||||
"is_global": reaction[6],
|
||||
"guild_id": reaction[7],
|
||||
"creator_id": reaction[8],
|
||||
"usage_count": reaction[9],
|
||||
"created_at": reaction[10],
|
||||
"updated_at": reaction[11],
|
||||
"type": "emoji" if reaction[4] else "text"
|
||||
}
|
||||
return None
|
||||
|
||||
async def find_all_by_guild(
|
||||
self, guild_id: int
|
||||
) -> List[Dict[str, Any]]:
|
||||
query = """
|
||||
SELECT * FROM custom_reactions
|
||||
WHERE guild_id = ?
|
||||
"""
|
||||
results = database.select_query(query, (guild_id,))
|
||||
reactions = []
|
||||
for reaction in results:
|
||||
reactions.append({
|
||||
"id": reaction[0],
|
||||
"trigger_text": reaction[1],
|
||||
"response": reaction[2],
|
||||
"emoji_id": reaction[3],
|
||||
"is_emoji": reaction[4],
|
||||
"is_full_match": reaction[5],
|
||||
"is_global": reaction[6],
|
||||
"guild_id": reaction[7],
|
||||
"creator_id": reaction[8],
|
||||
"usage_count": reaction[9],
|
||||
"created_at": reaction[10],
|
||||
"updated_at": reaction[11],
|
||||
"type": "emoji" if reaction[4] else "text"
|
||||
})
|
||||
return reactions
|
||||
|
||||
async def create_custom_reaction(
|
||||
self,
|
||||
guild_id: int,
|
||||
|
|
Loading…
Reference in a new issue