1
Fork 0
mirror of https://github.com/allthingslinux/tux.git synced 2024-10-02 16:43:12 +00:00
tux/.archive/notes.py
kzndotsh 56024ec2ff refactor(notes.py, dev.py, eval.py, git.py, mail.py): replace ctx.reply with ctx.send for better compatibility
feat(notes.py, dev.py, eval.py, git.py, mail.py): add ephemeral and delete_after parameters to ctx.send to improve user experience

refactor(random.py, xkcd.py, config.py, export.py, rolecount.py, avatar.py, run.py, ban.py): replace ctx.reply with ctx.send for better compatibility across different contexts
feat(random.py, config.py, avatar.py, run.py, ban.py): add delete_after parameter to auto delete messages after 30 seconds for cleaner chat
fix(rolecount.py): change fallback emoji from role.display_icon to "?" for better error handling
fix(avatar.py): add ephemeral parameter to make certain replies only visible to the command invoker

refactor(cases.py): remove commented code and unused function to improve code readability
refactor(cases.py): replace ctx.reply with ctx.send to ensure compatibility with different bot versions
feat(cases.py): add type hinting to emojis dictionary for better type safety
feat(cases.py): refactor emoji formatting and case description generation for better code organization
fix(jail.py): add condition to check existing permissions before setting new ones to avoid unnecessary API calls
refactor(jail.py): replace ctx.reply with ctx.send to ensure compatibility with different bot versions
style(jail.py): format interaction response to adhere to line length limit

refactor(moderation): replace ctx.reply with ctx.send for better compatibility across different contexts
feat(moderation): add delete_after and ephemeral options to interaction responses for better user experience
chore(moderation): remove unnecessary logging statements to clean up console output
feat(moderation/roles): add ephemeral and delete_after options to role assignment messages for better user experience

refactor(warn.py, poll.py, query.py, remindme.py, snippets.py, tldr.py, wiki.py): replace ctx.reply with ctx.send for consistency
feat(warn.py, poll.py, query.py, remindme.py, snippets.py): add delete_after and ephemeral parameters to ctx.send for better user experience
style(poll.py, tldr.py): remove unnecessary logger.info statements for cleaner code
fix(snippets.py): adjust permission level check for snippet deletion for better access control
feat(wiki.py): add alias 'wk' to wiki command for easier access
style(query.py): shorten footer text in embed message for brevity
2024-07-31 22:56:03 +00:00

227 lines
6.9 KiB
Python

import discord
from discord.ext import commands
from prisma.models import Note
from tux.utils.constants import Constants as CONST
from . import ModerationCogBase
class Notes(ModerationCogBase):
def __init__(self, bot: commands.Bot) -> None:
super().__init__(bot)
@commands.hybrid_group(
name="notes",
aliases=["n"],
usage="$notes <subcommand>",
)
@commands.guild_only()
async def notes(self, ctx: commands.Context[commands.Bot]) -> None:
"""
Notes related commands.
Parameters
----------
ctx : commands.Context[commands.Bot]
The context object for the command.
"""
if ctx.invoked_subcommand is None:
await ctx.send_help("notes")
@notes.command(
name="create",
aliases=["c", "add", "a"],
usage="$notes create [target] [content]",
)
@commands.guild_only()
async def create_note(self, ctx: commands.Context[commands.Bot], target: discord.Member, content: str) -> None:
"""
Create a note for a user.
Parameters
----------
ctx : commands.Context[commands.Bot]
The context in which the command is being invoked.
target : discord.Member
The member to add a note to.
content : str
The content of the note
"""
if ctx.guild:
try:
note = await self.db.note.insert_note(
note_target_id=target.id,
note_moderator_id=ctx.author.id,
note_content=content,
guild_id=ctx.guild.id,
)
except Exception as e:
await ctx.send(f"An error occurred while creating the note: {e}", delete_after=30, ephemeral=True)
return
await self.handle_note_response(ctx, note, "created", content, target)
@notes.command(
name="delete",
aliases=["d"],
usage="$notes delete [note_id]",
)
@commands.guild_only()
async def delete_note(self, ctx: commands.Context[commands.Bot], note_id: int) -> None:
"""
Delete a note by ID.
Parameters
----------
ctx : commands.Context[commands.Bot]
The context in which the command is being invoked.
note_id : int
The ID of the note to delete
Raises
------
commands.MemberNotFound
If the member is not found.
commands.UserNotFound
If the user is not found.
"""
if ctx.guild:
note = await self.db.note.get_note_by_id(note_id)
if not note:
await ctx.send("Note not found.", delete_after=30, ephemeral=True)
return
await self.db.note.delete_note_by_id(note_id)
try:
target = await commands.MemberConverter().convert(ctx, str(note.note_target_id))
except commands.MemberNotFound:
target = await commands.UserConverter().convert(ctx, str(note.note_target_id))
await self.handle_note_response(ctx, note, "deleted", note.note_content, target)
@notes.command(
name="update",
aliases=["u", "edit", "e", "modify", "m"],
usage="$notes update [note_id] [content]",
)
@commands.guild_only()
async def update_note(self, ctx: commands.Context[commands.Bot], note_id: int, content: str) -> None:
"""
Update a note by ID.
Parameters
----------
ctx : commands.Context[commands.Bot]
The context in which the command is being invoked.
note_id : int
The ID of the note to update.
content : str
The new content for the note.
Raises
------
commands.MemberNotFound
If the member is not found.
commands.UserNotFound
If the user is not found
"""
if ctx.guild:
note = await self.db.note.get_note_by_id(note_id)
if not note:
await ctx.send("Note not found.", delete_after=30, ephemeral=True)
return
previous_content = note.note_content
await self.db.note.update_note_by_id(note_id, content)
try:
target = await commands.MemberConverter().convert(ctx, str(note.note_target_id))
except commands.MemberNotFound:
target = await commands.UserConverter().convert(ctx, str(note.note_target_id))
await self.handle_note_response(ctx, note, "updated", content, target, previous_content)
@notes.command(
name="view",
aliases=["v", "get", "g"],
usage="$notes view [note_id]",
)
@commands.guild_only()
async def view_note(
self,
ctx: commands.Context[commands.Bot],
note_id: int,
) -> None:
"""
View a note by ID.
Parameters
----------
ctx : commands.Context[commands.Bot]
The context in which the command is being invoked.
note_id : int
The ID of the note to view.
Raises
------
commands.MemberNotFound
If the member is not found.
commands.UserNotFound
If the user is not found.
"""
if ctx.guild:
note = await self.db.note.get_note_by_id(note_id)
if not note:
await ctx.send("Note not found.", delete_after=30, ephemeral=True)
return
try:
target = await commands.MemberConverter().convert(ctx, str(note.note_target_id))
except commands.MemberNotFound:
target = await commands.UserConverter().convert(ctx, str(note.note_target_id))
await self.handle_note_response(ctx, note, "viewed", note.note_content, target)
async def handle_note_response(
self,
ctx: commands.Context[commands.Bot],
note: Note,
action: str,
content: str,
target: discord.Member | discord.User,
previous_content: str | None = None,
) -> None:
moderator = ctx.author
fields = [
("Moderator", f"__{moderator}__\n`{moderator.id}`", True),
("Target", f"__{target}__\n`{target.id}`", True),
("Content", f"> {content}", False),
]
if previous_content:
fields.append(("Previous Content", f"> {previous_content}", False))
embed = await self.create_embed(
ctx,
title=f"Note #{note.note_id} {action}",
fields=fields,
color=CONST.EMBED_COLORS["NOTE"],
icon_url=CONST.EMBED_ICONS["NOTE"],
)
await self.send_embed(ctx, embed, log_type="private")
await ctx.send(embed=embed, delete_after=30, ephemeral=True)
async def setup(bot: commands.Bot) -> None:
await bot.add_cog(Notes(bot))