1
Fork 0
mirror of https://github.com/wlinator/luminara.git synced 2024-10-02 18:23:12 +00:00

Refactor custom_reactions table column names for triggers and emojis

This commit is contained in:
wlinator 2024-07-11 09:30:28 -04:00
parent e9a7603a19
commit 26db690687
3 changed files with 49 additions and 22 deletions

View file

@ -3,12 +3,12 @@ CREATE TABLE custom_reactions (
id SERIAL PRIMARY KEY, -- Unique identifier for each custom reaction
trigger_text TEXT NOT NULL, -- The text that triggers the custom reaction
response TEXT, -- The response text for the custom reaction (nullable for emoji reactions)
emoji TEXT, -- The emoji for the custom reaction (nullable for text responses)
emoji_id BIGINT UNSIGNED, -- The emoji for the custom reaction (nullable for text responses)
is_emoji BOOLEAN DEFAULT FALSE, -- Indicates if the reaction is a discord emoji reaction
is_full_match BOOLEAN DEFAULT FALSE, -- Indicates if the trigger matches the full content of the message
is_global BOOLEAN DEFAULT TRUE, -- Indicates if the reaction is global or specific to a guild
guild_id BIGINT, -- The ID of the guild where the custom reaction is used (nullable for global reactions)
creator_id BIGINT NOT NULL, -- The ID of the user who created the custom reaction
guild_id BIGINT UNSIGNED, -- The ID of the guild where the custom reaction is used (nullable for global reactions)
creator_id BIGINT UNSIGNED NOT NULL, -- The ID of the user who created the custom reaction
usage_count INT DEFAULT 0, -- The number of times a custom reaction has been used
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, -- Timestamp when the custom reaction was created
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, -- Timestamp when the custom reaction was last updated

View file

@ -17,30 +17,57 @@ class ReactionHandler:
self.content: str = self.message.content.lower()
self.reaction_service = CustomReactionsService()
async def run_all_checks(self) -> None:
async def run_checks(self) -> None:
"""
Runs all checks for reactions and responses.
Runs checks for reactions and responses.
Guild triggers are prioritized over global triggers if they are identical.
"""
guild_id = self.message.guild.id if self.message.guild else None
if guild_id:
reaction = await self.reaction_service.find_trigger(guild_id, self.content)
if reaction:
data = await self.reaction_service.find_trigger(guild_id, self.content)
if data:
processed = False
try:
if reaction["type"] == "text":
await self.message.reply(reaction["response"])
processed = True
elif reaction["type"] == "emoji":
await self.message.add_reaction(reaction["response"])
processed = True
if data["type"] == "text":
processed = await self.try_respond(data)
elif data["type"] == "emoji":
processed = await self.try_react(data)
except Exception as e:
logger.warning(f"Failed to process reaction: {e}")
if processed:
await self.reaction_service.increment_reaction_usage(
int(reaction["id"])
int(data["id"])
)
async def try_respond(self, data) -> bool:
"""
Tries to respond to the message.
"""
response = data.get("response")
if response:
try:
await self.message.reply(response)
return True
except Exception:
pass
return False
async def try_react(self, data) -> bool:
"""
Tries to react to the message.
"""
emoji_id = data.get("emoji_id")
if emoji_id:
try:
emoji = self.client.get_emoji(emoji_id)
if emoji:
await self.message.add_reaction(emoji)
return True
except Exception:
pass
return False
class ReactionListener(Cog):
@ -57,7 +84,7 @@ class ReactionListener(Cog):
if not message.author.bot and not BlacklistUserService.is_user_blacklisted(
message.author.id
):
await ReactionHandler(self.client, message).run_all_checks()
await ReactionHandler(self.client, message).run_checks()
def setup(client) -> None:

View file

@ -27,7 +27,7 @@ class CustomReactionsService:
"id": reaction[0],
"trigger_text": reaction[1],
"response": reaction[2],
"emoji": reaction[3],
"emoji_id": reaction[3],
"is_emoji": reaction[4],
"is_full_match": reaction[5],
"is_global": reaction[6],
@ -46,7 +46,7 @@ class CustomReactionsService:
creator_id: int,
trigger_text: str,
response: Optional[str] = None,
emoji: Optional[str] = None,
emoji_id: Optional[int] = None,
is_emoji: bool = False,
is_full_match: bool = False,
is_global: bool = True,
@ -55,7 +55,7 @@ class CustomReactionsService:
return False
query = """
INSERT INTO custom_reactions (trigger_text, response, emoji, is_emoji, is_full_match, is_global, guild_id, creator_id)
INSERT INTO custom_reactions (trigger_text, response, emoji_id, is_emoji, is_full_match, is_global, guild_id, creator_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE trigger_text=trigger_text
"""
@ -64,7 +64,7 @@ class CustomReactionsService:
(
trigger_text,
response,
emoji,
emoji_id,
is_emoji,
is_full_match,
is_global,
@ -79,7 +79,7 @@ class CustomReactionsService:
guild_id: int,
trigger_text: str,
new_response: Optional[str] = None,
new_emoji: Optional[str] = None,
new_emoji_id: Optional[int] = None,
is_emoji: Optional[bool] = None,
is_full_match: Optional[bool] = None,
is_global: Optional[bool] = None,
@ -87,7 +87,7 @@ class CustomReactionsService:
query = """
UPDATE custom_reactions
SET response = COALESCE(?, response),
emoji = COALESCE(?, emoji),
emoji_id = COALESCE(?, emoji_id),
is_emoji = COALESCE(?, is_emoji),
is_full_match = COALESCE(?, is_full_match),
is_global = COALESCE(?, is_global),
@ -98,7 +98,7 @@ class CustomReactionsService:
query,
(
new_response,
new_emoji,
new_emoji_id,
is_emoji,
is_full_match,
is_global,