Improve functionality of tags
This commit is contained in:
parent
f7553f5c8f
commit
85bce061a1
2 changed files with 59 additions and 12 deletions
|
@ -1,6 +1,8 @@
|
|||
"""JARVIS Tags Cog."""
|
||||
import asyncio
|
||||
import re
|
||||
from datetime import datetime, timezone
|
||||
from typing import Dict, List
|
||||
|
||||
from jarvis_core.db import q
|
||||
from jarvis_core.db.models import Setting, Tag
|
||||
|
@ -24,6 +26,9 @@ invites = re.compile(
|
|||
|
||||
|
||||
class TagCog(Extension):
|
||||
def __init__(self, bot: Client):
|
||||
self.bot = bot
|
||||
self.cache: Dict[int, List[int]] = {}
|
||||
|
||||
tag = SlashCommand(name="tag", description="Create and manage custom tags")
|
||||
|
||||
|
@ -62,7 +67,7 @@ class TagCog(Extension):
|
|||
placeholder="Content to send here",
|
||||
style=TextStyles.PARAGRAPH,
|
||||
custom_id="content",
|
||||
max_length=1000,
|
||||
max_length=512,
|
||||
),
|
||||
],
|
||||
)
|
||||
|
@ -70,14 +75,21 @@ class TagCog(Extension):
|
|||
await ctx.send_modal(modal)
|
||||
try:
|
||||
response = await self.bot.wait_for_modal(modal, author=ctx.author.id, timeout=60 * 5)
|
||||
name = response.responses.get("name")
|
||||
name = response.responses.get("name").replace("`", "")
|
||||
content = response.responses.get("content")
|
||||
except asyncio.TimeoutError:
|
||||
return
|
||||
|
||||
noinvite = await Setting.find_one(q(guild=ctx.guild.id, setting="noinvite"))
|
||||
|
||||
if (invites.search(content) or invites.search(name)) and noinvite.value:
|
||||
if (
|
||||
(invites.search(content) or invites.search(name))
|
||||
and noinvite.value
|
||||
and not (
|
||||
ctx.author.has_permission(Permissions.ADMINISTRATOR)
|
||||
or ctx.author.has_permission(Permissions.MANAGE_MESSAGES)
|
||||
)
|
||||
):
|
||||
await response.send(
|
||||
"Listen, don't use this to try and bypass the rules", ephemeral=True
|
||||
)
|
||||
|
@ -113,6 +125,9 @@ class TagCog(Extension):
|
|||
)
|
||||
|
||||
await response.send(embeds=embed)
|
||||
if ctx.guild.id not in self.cache:
|
||||
self.cache[ctx.guild.id] = []
|
||||
self.cache[ctx.guild.id].append(tag.name)
|
||||
|
||||
@tag.subcommand(sub_cmd_name="edit", sub_cmd_description="Edit a tag")
|
||||
@slash_option(
|
||||
|
@ -123,6 +138,7 @@ class TagCog(Extension):
|
|||
required=True,
|
||||
)
|
||||
async def _edit(self, ctx: InteractionContext, name: str) -> None:
|
||||
old_name = name
|
||||
tag = await Tag.find_one(q(guild=ctx.guild.id, name=name))
|
||||
if not tag:
|
||||
await ctx.send("Tag not found", ephemeral=True)
|
||||
|
@ -149,7 +165,7 @@ class TagCog(Extension):
|
|||
value=tag.content,
|
||||
style=TextStyles.PARAGRAPH,
|
||||
custom_id="content",
|
||||
max_length=1000,
|
||||
max_length=512,
|
||||
),
|
||||
],
|
||||
)
|
||||
|
@ -157,14 +173,28 @@ class TagCog(Extension):
|
|||
await ctx.send_modal(modal)
|
||||
try:
|
||||
response = await self.bot.wait_for_modal(modal, author=ctx.author.id, timeout=60 * 5)
|
||||
name = response.responses.get("name")
|
||||
name = response.responses.get("name").replace("`", "")
|
||||
content = response.responses.get("content")
|
||||
except asyncio.TimeoutError:
|
||||
return
|
||||
|
||||
new_tag = await Tag.find_one(q(guild=ctx.guild.id, name=name))
|
||||
if new_tag and new_tag.id != tag.id:
|
||||
await ctx.send(
|
||||
"That tag name is used by another tag, choose another name", ephemeral=True
|
||||
)
|
||||
return
|
||||
|
||||
noinvite = await Setting.find_one(q(guild=ctx.guild.id, setting="noinvite"))
|
||||
|
||||
if (invites.search(content) or invites.search(name)) and noinvite.value:
|
||||
if (
|
||||
(invites.search(content) or invites.search(name))
|
||||
and noinvite.value
|
||||
and not (
|
||||
ctx.author.has_permission(Permissions.ADMINISTRATOR)
|
||||
or ctx.author.has_permission(Permissions.MANAGE_MESSAGES)
|
||||
)
|
||||
):
|
||||
await response.send(
|
||||
"Listen, don't use this to try and bypass the rules", ephemeral=True
|
||||
)
|
||||
|
@ -175,6 +205,8 @@ class TagCog(Extension):
|
|||
|
||||
tag.content = re.sub(r"\\?([@<])", r"\\\g<1>", content)
|
||||
tag.name = name
|
||||
tag.edited_at = datetime.now(tz=timezone.utc)
|
||||
tag.editor = ctx.author.id
|
||||
|
||||
await tag.commit()
|
||||
|
||||
|
@ -193,6 +225,9 @@ class TagCog(Extension):
|
|||
)
|
||||
|
||||
await response.send(embeds=embed)
|
||||
if tag.name not in self.cache[ctx.guild.id]:
|
||||
self.cache[ctx.guild.id].remove(old_name)
|
||||
self.cache[ctx.guild.id].append(tag.name)
|
||||
|
||||
@tag.subcommand(sub_cmd_name="delete", sub_cmd_description="Delete a tag")
|
||||
@slash_option(
|
||||
|
@ -218,6 +253,7 @@ class TagCog(Extension):
|
|||
|
||||
await tag.delete()
|
||||
await ctx.send(f"Tag `{name}` deleted")
|
||||
self.cache[ctx.guild.id].remove(tag.name)
|
||||
|
||||
@tag.subcommand(sub_cmd_name="info", sub_cmd_description="Get info on a tag")
|
||||
@slash_option(
|
||||
|
@ -233,12 +269,13 @@ class TagCog(Extension):
|
|||
await ctx.send("Tag not found", ephemeral=True)
|
||||
return
|
||||
|
||||
username, discrim, url = None, None, None
|
||||
username, discrim, url, mention = None, None, None, "Unknown User"
|
||||
author = await self.bot.fetch_user(tag.creator)
|
||||
if author:
|
||||
username = author.username
|
||||
discrim = author.discriminator
|
||||
url = author.display_avatar.url
|
||||
mention = author.mention
|
||||
|
||||
ts = int(tag.created_at.timestamp())
|
||||
|
||||
|
@ -249,11 +286,20 @@ class TagCog(Extension):
|
|||
EmbedField(name="Name", value=name),
|
||||
EmbedField(name="Content", value=tag.content),
|
||||
EmbedField(name="Created At", value=f"<t:{ts}:F>"),
|
||||
EmbedField(name="Created By", value=mention),
|
||||
],
|
||||
)
|
||||
if tag.edited_at:
|
||||
ets = int(tag.edited_at.timestamp())
|
||||
editor = await self.bot.fetch_user(tag.editor)
|
||||
emention = "Unknown User"
|
||||
if editor:
|
||||
emention = editor.mention
|
||||
embed.add_field(name="Edited At", value=f"<t:{ets}:F>")
|
||||
embed.add_field(name="Edited By", value=emention)
|
||||
|
||||
embed.set_author(
|
||||
name=f"{username}#{discrim}" if username else "Unknown Author",
|
||||
name=f"{username}#{discrim}" if username else "Unknown User",
|
||||
icon_url=url,
|
||||
)
|
||||
|
||||
|
@ -264,9 +310,10 @@ class TagCog(Extension):
|
|||
@_delete.autocomplete("name")
|
||||
@_info.autocomplete("name")
|
||||
async def _autocomplete(self, ctx: AutocompleteContext, name: str) -> None:
|
||||
if not self.cache.get(ctx.guild.id):
|
||||
tags = await Tag.find(q(guild=ctx.guild.id)).to_list(None)
|
||||
names = [tag.name for tag in tags]
|
||||
results = process.extract(name, names, limit=25)
|
||||
self.cache[ctx.guild.id] = [tag.name for tag in tags]
|
||||
results = process.extract(name, self.cache.get(ctx.guild.id), limit=25)
|
||||
choices = [{"name": r[0], "value": r[0]} for r in results]
|
||||
await ctx.send(choices=choices)
|
||||
|
||||
|
|
2
poetry.lock
generated
2
poetry.lock
generated
|
@ -435,7 +435,7 @@ umongo = "^3.1.0"
|
|||
type = "git"
|
||||
url = "https://git.zevaryx.com/stark-industries/jarvis/jarvis-core.git"
|
||||
reference = "main"
|
||||
resolved_reference = "4cece14cd9cd1604bf12845339fdb5f66b6c0719"
|
||||
resolved_reference = "fe24fce330cfd23a7af3834ef11b675780e6325d"
|
||||
|
||||
[[package]]
|
||||
name = "jinxed"
|
||||
|
|
Loading…
Add table
Reference in a new issue