Skip to content

Commit

Permalink
feat: add psychopass and dominator slash commandds
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonejt committed Sep 21, 2024
1 parent 96ed69c commit 58692ff
Show file tree
Hide file tree
Showing 12 changed files with 398 additions and 79 deletions.
13 changes: 7 additions & 6 deletions clients/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ class ACTION(IntEnum):
ATTR_PRETTY = {
"crime_coefficient_100": "crime coefficient >= 100",
"crime_coefficient_300": "crime coefficient >= 300",
ATTRIBUTE.TOXICITY: "toxicity",
ATTRIBUTE.SEVERE_TOXICITY: "severe toxicity",
ATTRIBUTE.IDENTITY_ATTACK: "identity attack",
ATTRIBUTE.INSULT: "insult",
ATTRIBUTE.PROFANITY: "profanity",
ATTRIBUTE.SEXUALLY_EXPLICIT: "sexually explicit",
ATTRIBUTE.TOXICITY.value: "toxicity",
ATTRIBUTE.SEVERE_TOXICITY.value: "severe toxicity",
ATTRIBUTE.IDENTITY_ATTACK.value: "identity attack",
ATTRIBUTE.INSULT.value: "insult",
ATTRIBUTE.THREAT.value: "threat",
ATTRIBUTE.PROFANITY.value: "profanity",
ATTRIBUTE.SEXUALLY_EXPLICIT.value: "sexually explicit",
}
151 changes: 151 additions & 0 deletions commands/dominator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
from typing import Union
from loguru import logger as log
from discord import (
ApplicationContext,
Bot,
Cog,
SlashCommandGroup,
option,
SlashCommandOptionType,
OptionChoice,
)

from clients.backend.dominator.member_dominators import MemberDominators
from clients.backend.dominator.message_dominators import MessageDominators
from clients.constants import ACTION, ATTR_PRETTY, ATTRIBUTE
from embeds.dominator import embed_dominator


class Dominator(Cog):

base_dominator = SlashCommandGroup(
"dominator", description="configure sibyl's automod capabilities"
)

def __init__(self, bot: Bot) -> None:
self.bot = bot

@base_dominator.command(description="configure message dominator")
@option(
"attribute",
type=SlashCommandOptionType.string,
description="attribute to set",
choices=list(
map(
lambda attr: OptionChoice(name=ATTR_PRETTY[attr.value], value=attr),
ATTRIBUTE.__members__.values(),
),
),
)
@option(
"action",
type=ACTION,
description="action to take upon exceeding threshold",
required=False,
)
@option(
"threshold",
type=SlashCommandOptionType.number,
description="threshold for attribute score",
required=False,
min_value=0,
max_value=1,
)
async def message(
self,
ctx: ApplicationContext,
attribute: str,
action: ACTION,
threshold: float,
) -> None:
if not ctx.author.guild_permissions.administrator:
await ctx.followup.send(
"you do not have permissions to configure notification settings"
)
return
await ctx.defer()

await self.configure_dominator(
ctx, MessageDominators, attribute, action, threshold
)

@base_dominator.command(description="configure member dominator")
@option(
"attribute",
type=SlashCommandOptionType.string,
description="attribute to set",
choices=list(
map(
lambda attr: OptionChoice(name=ATTR_PRETTY[attr], value=attr),
ATTR_PRETTY.keys(),
)
),
)
@option(
"action",
type=ACTION,
description="action to take upon exceeding threshold",
required=False,
)
@option(
"threshold",
type=SlashCommandOptionType.number,
description="threshold for attribute score",
required=False,
min_value=0,
max_value=1,
)
async def member(
self,
ctx: ApplicationContext,
attribute: str,
action: ACTION,
threshold: float,
) -> None:
if not ctx.author.guild_permissions.administrator:
await ctx.followup.send(
"you do not have permissions to configure notification settings"
)
return
await ctx.defer()

await self.configure_dominator(
ctx, MemberDominators, attribute, action, threshold
)

async def configure_dominator(
self,
ctx: ApplicationContext,
dominator: Union[MessageDominators, MemberDominators],
attribute: str,
action: ACTION,
threshold: float,
) -> None:
if attribute == "crime_coefficient_100" and action is not None:
dominator.update(
{"communityID": ctx.guild_id, "crime_coefficient_100_action": action}
)
elif attribute == "crime_coefficient_300" and action is not None:
dominator.update(
{"communityID": ctx.guild_id, "crime_coefficient_300_action": action}
)
else:
trigger_data = {"communityID": ctx.guild_id}
if action is not None:
trigger_data[f"{attribute}_action"] = action
if threshold is not None:
trigger_data[f"{attribute}_threshold"] = threshold

dominator.update(trigger_data)

await ctx.edit(
embed=embed_dominator(dominator.read(ctx.guild_id), attribute, ctx.guild)
)

log.info(
"{} trigger has been successfully updated for {} in server: {} ({})",
attribute,
dominator.__class__.__name__,
ctx.guild.name,
ctx.guild_id,
)
53 changes: 53 additions & 0 deletions commands/psychopass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from loguru import logger as log
from discord import (
ApplicationContext,
Bot,
Cog,
slash_command,
User,
SlashCommandOptionType,
option,
)

from clients.backend.psychopass.community_psycho_passes import CommmunityPsychoPasses
from clients.backend.psychopass.psycho_passes import PsychoPasses
from embeds.psycho_pass import embed_community_psycho_pass, embed_psycho_pass


class PsychoPass(Cog):

def __init__(self, bot: Bot) -> None:
self.bot = bot

@slash_command(description="get the psycho-pass of a user or server")
@option(
"user",
type=SlashCommandOptionType.user,
description="gets this user's psycho-pass",
required=False,
)
async def psychopass(self, ctx: ApplicationContext, user: User) -> None:
log.debug("{} /psychopass user: @{}", ctx.guild_id, user)
await ctx.defer()
if user is not None:
log.info(
"@{} ({}) has requested the psycho-pass of @{} ({})",
ctx.user.name,
ctx.user.id,
user.name,
user.id,
)
psycho_pass = PsychoPasses.read(user.id)
await ctx.edit(embed=embed_psycho_pass(psycho_pass, ctx.user, user))
else:
log.info(
"@{} ({}) has requested the area stress level of {} ({})",
ctx.user.name,
ctx.user.id,
ctx.guild.name,
ctx.guild_id,
)
psycho_pass = CommmunityPsychoPasses.read(ctx.guild_id)
await ctx.edit(
embed=embed_community_psycho_pass(psycho_pass, ctx.user, ctx.guild)
)
35 changes: 35 additions & 0 deletions embeds/dominator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from discord import Embed, EmbedAuthor, Guild

from clients.constants import ATTR_PRETTY, ATTRIBUTE, ACTION


def embed_dominator(dominator: dict, attribute: str, server: Guild) -> Embed:
embed = Embed(
description=f"{ATTR_PRETTY[attribute]} updated",
author=EmbedAuthor(name="sibylmod"),
thumbnail=server.icon.url,
)

if (
"crime_coefficient_100_action" in dominator.keys()
and "crime_coefficient_300_action" in dominator.keys()
):
embed.title = f"member dominator settings for server: {server.name}"
embed.add_field(
name=f"{ATTR_PRETTY['crime_coefficient_100']} action",
value=ACTION(dominator["crime_coefficient_100_action"]).name,
)
embed.add_field(
name=f"{ATTR_PRETTY['crime_coefficient_300']} action",
value=ACTION(dominator["crime_coefficient_300_action"]).name,
)
else:
embed.title = f"message dominator settings for server: {server.name}"

for attribute in ATTRIBUTE.__members__.values():
embed.add_field(
name=f"{ATTR_PRETTY[attribute]} action / threshold",
value=f"{ACTION(dominator[f'{attribute}_action']).name} / {dominator[f'{attribute}_threshold']}",
)

return embed
31 changes: 0 additions & 31 deletions embeds/member_moderation.py

This file was deleted.

34 changes: 0 additions & 34 deletions embeds/message_moderation.py

This file was deleted.

62 changes: 62 additions & 0 deletions embeds/moderation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from datetime import datetime, UTC
from discord import Embed, EmbedAuthor, EmbedFooter, Message, Member
from clients.constants import ACTION, ATTR_PRETTY


def embed_message_moderation(
message: Message, action: ACTION, reasons: list[tuple]
) -> Embed:
embed = Embed(
title=f"flagged message from {message.author.name}",
url=message.jump_url,
author=EmbedAuthor(name="sibylmod", icon_url=message.author.avatar.url),
timestamp=datetime.now(UTC),
footer=EmbedFooter(text=message.id),
)

if action is ACTION.BAN:
embed.description = f"banned {message.author.mention}"
elif action is ACTION.KICK:
embed.description = f"kicked {message.author.mention}"
elif action is ACTION.MUTE:
embed.description = f"muted {message.author.mention}"
elif action is ACTION.REMOVE:
embed.description = "removed message"
elif action is ACTION.NOTIFY:
embed.description = "notified moderators"

for reason in reasons:
attribute, score, threshold = reason
embed.add_field(
name=f"{ATTR_PRETTY[attribute]} / threshold", value=f"{score} / {threshold}"
)

return embed


def embed_member_moderation(
member: Member, action: ACTION, reasons: list[tuple]
) -> Embed:
embed = Embed(
title=f"flagged psycho-pass of {member.name}",
author=EmbedAuthor(name="sibylmod", icon_url=member.avatar.url),
timestamp=datetime.now(UTC),
footer=EmbedFooter(text=member.id),
)

if action is ACTION.BAN:
embed.description = f"banned {member.mention}"
elif action is ACTION.KICK:
embed.description = f"kicked {member.mention}"
elif action is ACTION.MUTE:
embed.description = f"muted {member.mention}"
elif action >= ACTION.NOTIFY:
embed.description = "notified moderators"

for reason in reasons:
attribute, score, threshold = reason
embed.add_field(
name=f"{ATTR_PRETTY[attribute]} / threshold", value=f"{score} / {threshold}"
)

return embed
Loading

0 comments on commit 58692ff

Please sign in to comment.