Add on_command listener

This commit is contained in:
Zeva Rose 2022-02-21 13:04:18 -07:00
parent 40fa82df66
commit 824548b04d
3 changed files with 41 additions and 4 deletions

View file

@ -18,7 +18,7 @@ file_handler = logging.FileHandler(filename="jarvis.log", encoding="UTF-8", mode
file_handler.setFormatter(logging.Formatter("[%(asctime)s][%(levelname)s][%(name)s] %(message)s"))
logger.addHandler(file_handler)
intents = Intents.DEFAULT | Intents.MESSAGES | Intents.GUILD_MEMBERS
intents = Intents.DEFAULT | Intents.MESSAGES | Intents.GUILD_MEMBERS | Intents.GUILD_MESSAGES
restart_ctx = None

View file

@ -4,7 +4,14 @@ import traceback
from datetime import datetime
from aiohttp import ClientSession
from dis_snek import Context, Snake, listen
from dis_snek import Snake, listen
from dis_snek.models.discord.channel import DMChannel
from dis_snek.models.discord.embed import EmbedField
from dis_snek.models.snek.context import Context, InteractionContext
from jarvis_core.db import q
from jarvis_core.db.models import Setting
from jarvis_core.util import build_embed
from jarvis_core.util.ansi import RESET, Fore, Format, fmt
DEFAULT_GUILD = 862402786116763668
DEFAULT_ERROR_CHANNEL = 943395824560394250
@ -23,6 +30,10 @@ Callback:
{callback_kwargs}
"""
KEY_FMT = fmt(Fore.GRAY)
VAL_FMT = fmt(Fore.WHITE)
CMD_FMT = fmt(Fore.GREEN, Format.BOLD)
class Jarvis(Snake):
@listen()
@ -35,6 +46,32 @@ class Jarvis(Snake):
"{}&permissions=8&scope=bot%20applications.commands".format(self.user.id)
)
async def on_command(self, ctx: InteractionContext) -> None:
"""Lepton on_command override."""
if not isinstance(ctx.channel, DMChannel) and ctx.invoked_name not in ["pw"]:
modlog = await Setting.find_one(q(guild=ctx.guild.id, setting="modlog"))
if modlog:
channel = await ctx.guild.fetch_channel(modlog.value)
args = " ".join(f"{KEY_FMT}{k}:{VAL_FMT}{v}{RESET}" for k, v in ctx.kwargs.items())
fields = [
EmbedField(
name="Command",
value=f"```ansi\n{CMD_FMT}{ctx.invoked_name}{RESET} {args}\n```",
inline=False,
),
]
embed = build_embed(
title="Command Invoked",
description=f"{ctx.author.mention} invoked a command",
fields=fields,
color="#fc9e3f",
)
embed.set_author(name=ctx.author.username, icon_url=ctx.author.display_avatar.url)
embed.set_footer(
text=f"{ctx.author.username}#{ctx.author.discriminator} | {ctx.author.id}"
)
await channel.send(embed=embed)
async def on_command_error(
self, ctx: Context, error: Exception, *args: list, **kwargs: dict
) -> None:

4
poetry.lock generated
View file

@ -214,7 +214,7 @@ plugins = ["setuptools"]
[[package]]
name = "jarvis-core"
version = "0.4.1"
version = "0.5.0"
description = ""
category = "main"
optional = false
@ -232,7 +232,7 @@ umongo = "^3.1.0"
type = "git"
url = "https://git.zevaryx.com/stark-industries/jarvis/jarvis-core.git"
reference = "main"
resolved_reference = "20f67444579d36d508def2b47ea826af9bbdd2d7"
resolved_reference = "f392757bb773cfeb9b3f14d581a599d13ee6e891"
[[package]]
name = "jedi"