Skip to content

Commit

Permalink
Fix AI interaction bug and add moderation setup check
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaUrsa committed Feb 28, 2024
1 parent e55d64f commit 4364479
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 34 deletions.
12 changes: 12 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
"bottest",
"CLEARMIND",
"CLOSEDTRIPSIT",
"codepaths",
"codespace",
"CODESPACE",
"COLLABVC",
"combochart",
"COMPSCI",
Expand All @@ -43,6 +46,7 @@
"DONATIONTRIGGER",
"downvotes",
"DRUGQUESTIONS",
"editreply",
"EROWID",
"Factsheets",
"GLITCHTIP",
Expand Down Expand Up @@ -79,11 +83,14 @@
"moodle",
"Moonbear",
"MSGLOG",
"napi",
"NEEDSHELP",
"NOSONAR",
"openai",
"OPENTRIPSIT",
"peronsas",
"pgadmin",
"PGADMIN",
"PSYCHONAUT",
"REALTALK",
"ROLLBAR",
Expand Down Expand Up @@ -121,7 +128,12 @@
"TRIPTOWNNOTICES",
"TRIVIABIGBRAIN",
"tryptamines",
"tsbuildinfo",
"Unban",
"UNDERBAN",
"underbanning",
"Underbans",
"Untimeout",
"upvotes",
"Utaninja",
"VIPLOUNGE",
Expand Down
4 changes: 2 additions & 2 deletions src/discord/commands/global/d.ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ async function createPersona(
if (i.customId.split('~')[1] !== interaction.id) return;
if (!i.isModalSubmit()) return;
if (!i.isFromMessage()) return;
// await i.deferUpdate();
await i.deferUpdate();

const personaName = i.fields.getTextInputValue('name');
const temperature = parseFloat(i.fields.getTextInputValue('temperature'));
Expand Down Expand Up @@ -918,7 +918,7 @@ async function createPersona(
},
});

await i.update({
await i.editReply({
embeds: [embedTemplate()
.setTitle('Modal')
.setColor(Colors.Green)
Expand Down
75 changes: 43 additions & 32 deletions src/discord/commands/guild/d.moderate.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable sonarjs/cognitive-complexity */
/* eslint-disable sonarjs/no-duplicate-string */
/* eslint-disable max-len */
import {
Expand Down Expand Up @@ -63,6 +64,7 @@ type UndoAction = 'UN-FULL_BAN' | 'UN-TICKET_BAN' | 'UN-DISCORD_BOT_BAN' | 'UN-B

type ModAction = user_action_type | UndoAction | 'INFO' | 'LINK';
// type BanAction = 'FULL_BAN' | 'TICKET_BAN' | 'DISCORD_BOT_BAN' | 'BAN_EVASION' | 'UNDERBAN';
type TargetObject = Snowflake | User | GuildMember;

const disableButtonTime = env.NODE_ENV !== 'production' ? 1000 * 60 * 1 : 1000 * 60 * 5; // 1 minute in dev, 5 minute in prod

Expand Down Expand Up @@ -247,11 +249,11 @@ const warnButtons = new ActionRowBuilder<ButtonBuilder>().addComponents(
);

function isSnowflake(id: string): boolean {
return /^[0-9]{17,19}$/.test(id);
return /^\d{17,19}$/.test(id);
}

function isMention(id: string): boolean {
return /^<@!?[0-9]{17,19}>$/.test(id);
return /^<@!?\d{17,19}>$/.test(id);
}

// Various action type checks
Expand Down Expand Up @@ -390,7 +392,7 @@ export async function tripSitTrustScore(
trustScore: number;
tsReasoning: string;
}> {
const startTime = Date.now();
// const startTime = Date.now();
let trustScore = 0;
let tsReasoning = '';
const targetPromise = discordClient.users.fetch(targetId);
Expand Down Expand Up @@ -568,7 +570,7 @@ export async function tripSitTrustScore(
`;
}

log.debug(F, `[trust score] time: ${Date.now() - startTime}ms`);
// log.debug(F, `[trust score] time: ${Date.now() - startTime}ms`);
return {
trustScore,
tsReasoning,
Expand All @@ -582,8 +584,8 @@ export async function userInfoEmbed(
command: ModAction,
showModInfo: boolean,
):Promise<EmbedBuilder> {
log.debug(F, `[userInfoEmbed] actor: ${actor} | target: ${target} | targetData: ${targetData} | command: ${command}`);
const startTime = Date.now();
log.debug(F, `[userInfoEmbed] actor: ${actor} | target: ${target} | targetData: ${JSON.stringify(targetData, null, 2)} | command: ${command}`);
// const startTime = Date.now();
const targetActionList = {
NOTE: [] as string[],
WARNING: [] as string[],
Expand Down Expand Up @@ -619,7 +621,7 @@ export async function userInfoEmbed(

log.debug(F, `Target: ${JSON.stringify(target, null, 2)}`);

const targetDisplayName = (target as GuildMember).displayName ?? null;
// const targetDisplayName = (target as GuildMember).displayName ?? null;
// let targetUserName = null as string | null;
// if ((target as GuildMember).user) {
// targetUserName = (target as GuildMember).user.username;
Expand All @@ -628,7 +630,7 @@ export async function userInfoEmbed(
// targetUserName = (target as User).username;
// }

log.debug(F, `targetDisplayName: ${targetDisplayName}`);
// log.debug(F, `targetDisplayName: ${targetDisplayName}`);

const targetId = (target as User | GuildMember).id ?? target;

Expand Down Expand Up @@ -763,7 +765,7 @@ export async function userInfoEmbed(
}
}

log.debug(F, `[userInfoEmbed] time: ${Date.now() - startTime}ms`);
// log.debug(F, `[userInfoEmbed] time: ${Date.now() - startTime}ms`);
// log.debug(F, `modlogEmbed: ${JSON.stringify(modlogEmbed, null, 2)}`);
return modlogEmbed;
}
Expand Down Expand Up @@ -821,7 +823,7 @@ export async function modResponse(
if ((isSnowflake(targetString) || isMention(targetString))) {
const userId = isSnowflake(targetString) ? targetString : targetString.replace(/[<@!>]/g, '');

let targetObj = userId as Snowflake | User | GuildMember;
let targetObj = userId as TargetObject;
try {
targetObj = await actor.guild.members.fetch(userId);
} catch (err) {
Expand Down Expand Up @@ -1032,7 +1034,7 @@ async function messageModThread(
extraMessage: string,
duration: string,
): Promise<ThreadChannel | null> {
// log.debug(F, `[messageModThread] actor: ${actor} | target: ${target} | command: ${command} | internalNote: ${internalNote} | description: ${description} | extraMessage: ${extraMessage} | duration: ${duration}`);
log.debug(F, `[messageModThread] actor: ${actor} | target: ${target} | command: ${command} | internalNote: ${internalNote} | description: ${description} | extraMessage: ${extraMessage} | duration: ${duration}`);
const startTime = Date.now();
const targetId = (target as User | GuildMember).id ?? target;
const targetName = (target as GuildMember).displayName ?? (target as User).username ?? target;
Expand All @@ -1042,15 +1044,19 @@ async function messageModThread(
create: { discord_id: targetId },
update: { },
});
log.debug(F, `targetData: ${JSON.stringify(targetData, null, 2)}`);
const guildData = await db.discord_guilds.upsert({
where: { id: actor.guild.id },
create: { id: actor.guild.id },
update: { },
});
log.debug(F, `guildData: ${JSON.stringify(guildData, null, 2)}`);

if (!guildData.channel_moderators) throw new Error('Moderator log room id is null');
if (!guildData.channel_moderators) return null;

if (!guildData.channel_mod_log) throw new Error('Moderator log room id is null');
if (!guildData.channel_mod_log) return null;

log.debug(F, 'Values are set, continuing');

const { pastVerb, emoji } = embedVariables[command as keyof typeof embedVariables];
let summary = `${actor.displayName} ${pastVerb} ${targetName}`;
Expand Down Expand Up @@ -1120,7 +1126,7 @@ async function messageModThread(
modThread = await modChan.threads.create({
name: `${emoji}${targetName}`,
autoArchiveDuration: 60,
}) as ThreadChannel;
});
// log.debug(F, 'created mod thread');
// Save the thread id to the user
targetData.mod_thread_id = modThread.id;
Expand Down Expand Up @@ -1311,7 +1317,7 @@ export async function moderate(
}

let targetName = targetId;
let targetObj = targetId as Snowflake | User | GuildMember;
let targetObj = targetId as TargetObject;
if (targetMember) {
targetName = targetMember.displayName;
targetObj = targetMember;
Expand Down Expand Up @@ -1372,7 +1378,7 @@ export async function moderate(
return { content: 'Timeout must be between 0 and 7 days!!' };
}

// convert the miliseconds into a human readable string
// convert the milliseconds into a human readable string
const humanTime = msToHuman(duration);

durationStr = `for ${humanTime}. It will expire ${time(new Date(Date.now() + duration), 'R')}`;
Expand Down Expand Up @@ -1454,7 +1460,7 @@ export async function moderate(
if (isFullBan(command)) {
const response = await last(
targetUser ?? targetMember?.user as User,
buttonInt.guild as Guild,
buttonInt.guild,
);
const extraMessage = `${targetName}'s last ${response.messageCount} (out of ${response.totalMessages}) messages before being banned :\n${response.messageList}`; // eslint-disable-line max-len
body = stripIndents`${body}\n\n${extraMessage}`;
Expand Down Expand Up @@ -1484,7 +1490,7 @@ export async function moderate(
log.debug(F, `Sending message to ${targetName}`);
await messageUser(
targetUser ?? targetMember?.user as User,
buttonInt.guild as Guild,
buttonInt.guild,
command,
body,
isTimeout(command) || isWarning(command),
Expand All @@ -1505,7 +1511,7 @@ export async function moderate(
created_at: new Date(),
} as user_actions;

// log.debug(F, `[moderate] performing actions for ${targetName}`);
log.debug(F, `[moderate] performing actions for ${targetName}`);
let extraMessage = '';
if (isBan(command)) {
if (isFullBan(command) || isUnderban(command) || isBanEvasion(command)) {
Expand Down Expand Up @@ -1607,7 +1613,7 @@ export async function moderate(

try {
await targetMember.timeout(0, internalNote ?? noReason);
// log.debug(F, `I untimeouted ${target.displayName} because\n '${internalNote}'!`);
// log.debug(F, `I untimeout ${target.displayName} because\n '${internalNote}'!`);
} catch (err) {
log.error(F, `Error: ${err}`);
}
Expand All @@ -1628,7 +1634,8 @@ export async function moderate(
}

// This needs to happen before creating the modlog embed
// await useractionsSet(actionData);
// await userActionsSet(actionData);
log.debug(F, `Updating user actions: ${JSON.stringify(actionData, null, 2)}`);
if (actionData.id) {
await db.user_actions.upsert({
where: { id: actionData.id },
Expand All @@ -1639,14 +1646,15 @@ export async function moderate(
await db.user_actions.create({ data: actionData });
}

log.debug(F, `Updating target data: ${JSON.stringify(targetData, null, 2)}`);
await db.users.update({
where: { id: targetData.id },
data: targetData,
});

const anonSummary = `${targetName} was ${embedVariables[command as keyof typeof embedVariables].pastVerb}${durationStr}!`;

log.debug(F, '[moderate] Sending messages');
log.debug(F, 'Sending message to mod thread');
const modThread = await messageModThread(
buttonInt,
actor,
Expand All @@ -1658,18 +1666,18 @@ export async function moderate(
durationStr,
);

// Records the action taken on the action field of the modlog embed
const embed = buttonInt.message.embeds[0].toJSON();
const actionField = embed.fields?.find(field => field.name === 'Actions');

if (actionField) {
// Add the action to the list of actions
// Add the action to the list of actions
const newActionFiled = actionField?.value.concat(`
${buttonInt.user.toString()} muted this user:
> ${modalInt.fields.getTextInputValue('internalNote')}
Message sent to user:
> ${modalInt.fields.getTextInputValue('description')}`);
${buttonInt.user.toString()} muted this user:
> ${modalInt.fields.getTextInputValue('internalNote')}
Message sent to user:
> ${modalInt.fields.getTextInputValue('description')}`);
// log.debug(F, `newActionFiled: ${newActionFiled}`);

// Replace the action field with the new one
Expand Down Expand Up @@ -1709,6 +1717,8 @@ export async function moderate(
.setFooter(null);

if (command !== 'REPORT' && modThread) response.setDescription(`${response.data.description}\nYou can access their thread here: ${modThread}`);

log.debug(F, `Returning embed: ${JSON.stringify(response, null, 2)}`);
return { embeds: [response] };
}

Expand Down Expand Up @@ -1753,7 +1763,7 @@ export async function modModal(
// update: {},
// });

let targetObj = userId as Snowflake | User | GuildMember;
let targetObj = userId as TargetObject;
try {
targetObj = await interaction.guild.members.fetch(userId);
} catch (err) {
Expand Down Expand Up @@ -2043,6 +2053,7 @@ export async function modModal(
await interaction.message.edit({
components: [buttonRow1],
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error:any) {
// This will happen on the initial ephemeral message and idk why
// log.error(F, `Error: ${error}`);
Expand Down Expand Up @@ -2106,7 +2117,7 @@ export const mod: SlashCommand = {

if (isLink(command)) {
const targetString = interaction.options.getString('target', true);
const targets = await getDiscordMember(interaction, targetString) as GuildMember[];
const targets = await getDiscordMember(interaction, targetString);
const override = interaction.options.getBoolean('override');
if (targets.length > 1) {
const embed = embedTemplate()
Expand Down
12 changes: 12 additions & 0 deletions src/discord/commands/guild/d.report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ChatInputCommandInteraction,
GuildMember,
} from 'discord.js';
import { stripIndents } from 'common-tags';
import { SlashCommand } from '../../@types/commandDef';
import commandContext from '../../utils/context';
import { modResponse } from './d.moderate';
Expand Down Expand Up @@ -36,6 +37,17 @@ export const dReport: SlashCommand = {
},
});

if (!guildData.role_moderator || !guildData.channel_mod_log || !guildData.channel_moderators) {
await interaction.editReply(stripIndents`
This server has not been set up for moderation.
Please contact an administrator to set up moderation.
If you are the admin, please use /cooperative to set up moderation.
`);
return false;
}

// Get the actor
const actor = interaction.member as GuildMember;
// Determine if the actor is a mod
Expand Down

0 comments on commit 4364479

Please sign in to comment.