2024-07-11 17:34:08 +00:00
|
|
|
from typing import Optional, Dict, Any, List
|
2024-07-10 19:03:20 +00:00
|
|
|
from datetime import datetime
|
|
|
|
from db import database
|
|
|
|
|
|
|
|
|
|
|
|
class CustomReactionsService:
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
async def find_trigger(
|
|
|
|
self, guild_id: int, message_content: str
|
|
|
|
) -> Optional[Dict[str, Any]]:
|
|
|
|
message_content = message_content.lower()
|
|
|
|
query = """
|
|
|
|
SELECT * FROM custom_reactions
|
|
|
|
WHERE (guild_id = ? OR is_global = TRUE) AND (
|
2024-07-10 20:11:12 +00:00
|
|
|
(is_full_match = TRUE AND trigger_text = ?) OR
|
|
|
|
(is_full_match = FALSE AND ? LIKE CONCAT('%', trigger_text, '%'))
|
2024-07-10 19:03:20 +00:00
|
|
|
)
|
|
|
|
ORDER BY guild_id = ? DESC, is_global ASC
|
|
|
|
LIMIT 1
|
|
|
|
"""
|
2024-07-10 20:11:12 +00:00
|
|
|
result = database.select_query(query, (guild_id, message_content, message_content, guild_id))
|
2024-07-10 19:03:20 +00:00
|
|
|
if result:
|
2024-07-10 20:11:12 +00:00
|
|
|
reaction = result[0] # Get the first result from the list
|
2024-07-10 19:03:20 +00:00
|
|
|
return {
|
2024-07-10 20:11:12 +00:00
|
|
|
"id": reaction[0],
|
|
|
|
"trigger_text": reaction[1],
|
|
|
|
"response": reaction[2],
|
2024-07-11 13:30:28 +00:00
|
|
|
"emoji_id": reaction[3],
|
2024-07-10 20:11:12 +00:00
|
|
|
"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"
|
2024-07-10 19:03:20 +00:00
|
|
|
}
|
|
|
|
return None
|
|
|
|
|
2024-07-11 17:34:08 +00:00
|
|
|
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
|
|
|
|
|
2024-07-10 19:03:20 +00:00
|
|
|
async def create_custom_reaction(
|
|
|
|
self,
|
|
|
|
guild_id: int,
|
|
|
|
creator_id: int,
|
2024-07-10 20:11:12 +00:00
|
|
|
trigger_text: str,
|
2024-07-10 19:03:20 +00:00
|
|
|
response: Optional[str] = None,
|
2024-07-11 13:30:28 +00:00
|
|
|
emoji_id: Optional[int] = None,
|
2024-07-10 19:03:20 +00:00
|
|
|
is_emoji: bool = False,
|
|
|
|
is_full_match: bool = False,
|
|
|
|
is_global: bool = True,
|
|
|
|
) -> bool:
|
|
|
|
if await self.count_custom_reactions(guild_id) >= 100:
|
|
|
|
return False
|
|
|
|
|
|
|
|
query = """
|
2024-07-11 13:30:28 +00:00
|
|
|
INSERT INTO custom_reactions (trigger_text, response, emoji_id, is_emoji, is_full_match, is_global, guild_id, creator_id)
|
2024-07-10 19:03:20 +00:00
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
2024-07-10 20:11:12 +00:00
|
|
|
ON DUPLICATE KEY UPDATE trigger_text=trigger_text
|
2024-07-10 19:03:20 +00:00
|
|
|
"""
|
|
|
|
database.execute_query(
|
|
|
|
query,
|
|
|
|
(
|
2024-07-10 20:11:12 +00:00
|
|
|
trigger_text,
|
2024-07-10 19:03:20 +00:00
|
|
|
response,
|
2024-07-11 13:30:28 +00:00
|
|
|
emoji_id,
|
2024-07-10 19:03:20 +00:00
|
|
|
is_emoji,
|
|
|
|
is_full_match,
|
|
|
|
is_global,
|
|
|
|
guild_id,
|
|
|
|
creator_id,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
return True
|
|
|
|
|
|
|
|
async def edit_custom_reaction(
|
|
|
|
self,
|
2024-07-11 16:47:46 +00:00
|
|
|
reaction_id: int,
|
2024-07-10 19:03:20 +00:00
|
|
|
new_response: Optional[str] = None,
|
2024-07-11 13:30:28 +00:00
|
|
|
new_emoji_id: Optional[int] = None,
|
2024-07-10 19:03:20 +00:00
|
|
|
is_emoji: Optional[bool] = None,
|
|
|
|
is_full_match: Optional[bool] = None,
|
|
|
|
is_global: Optional[bool] = None,
|
|
|
|
) -> bool:
|
|
|
|
query = """
|
|
|
|
UPDATE custom_reactions
|
|
|
|
SET response = COALESCE(?, response),
|
2024-07-11 13:30:28 +00:00
|
|
|
emoji_id = COALESCE(?, emoji_id),
|
2024-07-10 19:03:20 +00:00
|
|
|
is_emoji = COALESCE(?, is_emoji),
|
|
|
|
is_full_match = COALESCE(?, is_full_match),
|
|
|
|
is_global = COALESCE(?, is_global),
|
|
|
|
updated_at = ?
|
2024-07-11 16:47:46 +00:00
|
|
|
WHERE id = ?
|
2024-07-10 19:03:20 +00:00
|
|
|
"""
|
|
|
|
database.execute_query(
|
|
|
|
query,
|
|
|
|
(
|
|
|
|
new_response,
|
2024-07-11 13:30:28 +00:00
|
|
|
new_emoji_id,
|
2024-07-10 19:03:20 +00:00
|
|
|
is_emoji,
|
|
|
|
is_full_match,
|
|
|
|
is_global,
|
|
|
|
datetime.utcnow(),
|
2024-07-11 16:47:46 +00:00
|
|
|
reaction_id,
|
2024-07-10 19:03:20 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
return True
|
|
|
|
|
2024-07-11 16:47:46 +00:00
|
|
|
async def delete_custom_reaction(self, reaction_id: int) -> bool:
|
2024-07-10 19:03:20 +00:00
|
|
|
query = """
|
|
|
|
DELETE FROM custom_reactions
|
2024-07-11 16:47:46 +00:00
|
|
|
WHERE id = ?
|
2024-07-10 19:03:20 +00:00
|
|
|
"""
|
2024-07-11 16:47:46 +00:00
|
|
|
database.execute_query(query, (reaction_id,))
|
2024-07-10 19:03:20 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
async def count_custom_reactions(self, guild_id: int) -> int:
|
|
|
|
query = """
|
|
|
|
SELECT COUNT(*) FROM custom_reactions
|
|
|
|
WHERE guild_id = ?
|
|
|
|
"""
|
|
|
|
count = database.select_query_one(query, (guild_id,))
|
|
|
|
return count if count else 0
|
|
|
|
|
2024-07-10 20:11:12 +00:00
|
|
|
async def increment_reaction_usage(self, reaction_id: int) -> bool:
|
2024-07-10 19:03:20 +00:00
|
|
|
query = """
|
|
|
|
UPDATE custom_reactions
|
2024-07-10 20:11:12 +00:00
|
|
|
SET usage_count = usage_count + 1
|
|
|
|
WHERE id = ?
|
2024-07-10 19:03:20 +00:00
|
|
|
"""
|
|
|
|
database.execute_query(
|
|
|
|
query,
|
|
|
|
(
|
2024-07-10 20:11:12 +00:00
|
|
|
reaction_id,
|
2024-07-10 19:03:20 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
return True
|