Skip to content

Commit

Permalink
Move dm punishment to special column
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianVennen committed Jul 8, 2024
1 parent b39a91f commit d29acc8
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 67 deletions.
10 changes: 7 additions & 3 deletions src/automod/AutoModManager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Collection, PermissionFlagsBits, RESTJSONErrorCodes, ThreadChannel, userMention} from 'discord.js';
import {bold, Collection, PermissionFlagsBits, RESTJSONErrorCodes, ThreadChannel, userMention} from 'discord.js';
import GuildSettings from '../settings/GuildSettings.js';
import bot from '../bot/Bot.js';
import MemberWrapper from '../discord/MemberWrapper.js';
Expand Down Expand Up @@ -217,13 +217,17 @@ export class AutoModManager {
const reason = 'Using forbidden words or phrases';
const comment = `(Filter ID: ${word.id})`;
await bot.delete(message, reason + ' ' + comment);
if (word.response !== 'disabled' && word.punishment.action.toLowerCase() !== 'dm') {
if (word.response !== 'disabled') {
await this.#sendWarning(message, word.getResponse());
}

const member = new Member(message.author, message.guild);
if (word.punishment.action !== 'none') {
const member = new Member(message.author, message.guild);
await member.executePunishment(word.punishment, reason, comment);
}
if (word.dm) {
await member.guild.sendDM(member.user, `Your message in ${bold(message.guild.name)} was removed: ` + word.dm);
}
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/bot/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import CommentFieldMigration from '../database/migrations/CommentFieldMigration.
import {asyncFilter} from '../util/util.js';
import BadWordVisionMigration from '../database/migrations/BadWordVisionMigration.js';
import AutoResponseVisionMigration from '../database/migrations/AutoResponseVisionMigration.js';
import DMMigration from '../database/migrations/DMMigration.js';

export class Database {
/**
Expand Down Expand Up @@ -103,7 +104,7 @@ export class Database {
await this.query('CREATE TABLE IF NOT EXISTS `guilds` (`id` VARCHAR(20) NOT NULL, `config` TEXT NOT NULL, PRIMARY KEY (`id`))');
await this.query('CREATE TABLE IF NOT EXISTS `users` (`id` VARCHAR(20) NOT NULL, `config` TEXT NOT NULL, PRIMARY KEY (`id`))');
await this.query('CREATE TABLE IF NOT EXISTS `responses` (`id` int PRIMARY KEY AUTO_INCREMENT, `guildid` VARCHAR(20) NOT NULL, `trigger` TEXT NOT NULL, `response` TEXT NOT NULL, `global` BOOLEAN NOT NULL, `channels` TEXT NULL DEFAULT NULL, `enableVision` BOOLEAN DEFAULT FALSE)');
await this.query('CREATE TABLE IF NOT EXISTS `badWords` (`id` int PRIMARY KEY AUTO_INCREMENT, `guildid` VARCHAR(20) NOT NULL, `trigger` TEXT NOT NULL, `punishment` TEXT NOT NULL, `response` TEXT NOT NULL, `global` BOOLEAN NOT NULL, `channels` TEXT NULL DEFAULT NULL, `priority` int NULL, `enableVision` BOOLEAN DEFAULT FALSE)');
await this.query('CREATE TABLE IF NOT EXISTS `badWords` (`id` int PRIMARY KEY AUTO_INCREMENT, `guildid` VARCHAR(20) NOT NULL, `trigger` TEXT NOT NULL, `punishment` TEXT NOT NULL, `response` TEXT NOT NULL, `global` BOOLEAN NOT NULL, `channels` TEXT NULL DEFAULT NULL, `priority` int NULL, `dm` TEXT NULL DEFAULT NULL, `enableVision` BOOLEAN DEFAULT FALSE)');
await this.query('CREATE TABLE IF NOT EXISTS `moderations` (`id` int PRIMARY KEY AUTO_INCREMENT, `guildid` VARCHAR(20) NOT NULL, `userid` VARCHAR(20) NOT NULL, `action` VARCHAR(10) NOT NULL, `created` bigint NOT NULL, `value` int DEFAULT 0, `expireTime` bigint NULL DEFAULT NULL, `reason` TEXT, `comment` TEXT NULL DEFAULT NULL, `moderator` VARCHAR(20) NULL DEFAULT NULL, `active` BOOLEAN DEFAULT TRUE)');
await this.query('CREATE TABLE IF NOT EXISTS `confirmations` (`id` int PRIMARY KEY AUTO_INCREMENT, `data` TEXT NOT NULL, `expires` bigint NOT NULL)');
await this.query('CREATE TABLE IF NOT EXISTS `safeSearch` (`hash` CHAR(64) PRIMARY KEY, `data` TEXT NOT NULL)');
Expand All @@ -114,6 +115,7 @@ export class Database {
new CommentFieldMigration(this),
new BadWordVisionMigration(this),
new AutoResponseVisionMigration(this),
new DMMigration(this),
], async migration => await migration.check());
}

Expand Down
55 changes: 38 additions & 17 deletions src/commands/settings/bad-word/AddBadWordCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ export default class AddBadWordCommand extends AddAutoResponseCommand {
}, {
name: 'Strike user',
value: 'strike'
}, {
name: 'Send direct message',
value: 'DM'
}
)
);
Expand Down Expand Up @@ -93,12 +90,25 @@ export default class AddBadWordCommand extends AddAutoResponseCommand {
new TextInputBuilder()
.setRequired(false)
.setCustomId('priority')
.setStyle(TextInputStyle.Paragraph)
.setStyle(TextInputStyle.Short)
.setPlaceholder('0')
.setLabel('Priority')
.setMinLength(1)
.setMaxLength(10)
)
),
/** @type {*} */
new ActionRowBuilder()
.addComponents(
/** @type {*} */
new TextInputBuilder()
.setRequired(false)
.setCustomId('dm')
.setStyle(TextInputStyle.Paragraph)
.setPlaceholder('This is a direct message sent to the user when their message was deleted')
.setLabel('Direct Message')
.setMinLength(1)
.setMaxLength(3000)
),
);

if (['ban', 'mute'].includes(punishment)) {
Expand Down Expand Up @@ -131,20 +141,25 @@ export default class AddBadWordCommand extends AddAutoResponseCommand {
return;
}

let trigger, response = null, duration = null, priority = 0;
let trigger, response = null, duration = null, priority = 0, dm = null;
for (let component of interaction.components) {
component = component.components[0];
if (component.customId === 'trigger') {
trigger = component.value;
}
else if (component.customId === 'response') {
response = component.value?.substring?.(0, 4000);
}
else if (component.customId === 'duration') {
duration = parseTime(component.value) || null;
}
else if (component.customId === 'priority') {
priority = parseInt(component.value) || 0;
switch (component.customId) {
case 'trigger':
trigger = component.value;
break;
case 'response':
response = component.value?.substring?.(0, 4000);
break;
case 'duration':
duration = parseTime(component.value) || null;
break;
case 'priority':
priority = parseInt(component.value) || 0;
break;
case 'dm':
dm = component.value?.substring?.(0, 3000);
break;
}
}

Expand All @@ -160,13 +175,15 @@ export default class AddBadWordCommand extends AddAutoResponseCommand {
confirmation.data.punishment,
duration,
priority,
dm,
confirmation.data.vision,
);
} else {
confirmation.data.trigger = trigger;
confirmation.data.response = response;
confirmation.data.duration = duration;
confirmation.data.priority = priority;
confirmation.data.dm = dm;
confirmation.expires = timeAfter('30 min');

await interaction.reply({
Expand Down Expand Up @@ -209,6 +226,7 @@ export default class AddBadWordCommand extends AddAutoResponseCommand {
confirmation.data.punishment,
confirmation.data.duration,
confirmation.data.priority,
confirmation.data.dm,
confirmation.data.vision,
);
}
Expand All @@ -225,6 +243,7 @@ export default class AddBadWordCommand extends AddAutoResponseCommand {
* @param {?string} punishment
* @param {?number} duration
* @param {?number} priority
* @param {?string} dm
* @param {?boolean} enableVision
* @return {Promise<*>}
*/
Expand All @@ -238,6 +257,7 @@ export default class AddBadWordCommand extends AddAutoResponseCommand {
punishment,
duration,
priority,
dm,
enableVision,
) {
const result = await BadWord.new(
Expand All @@ -250,6 +270,7 @@ export default class AddBadWordCommand extends AddAutoResponseCommand {
punishment,
duration,
priority,
dm,
enableVision,
);
if (!result.success) {
Expand Down
53 changes: 36 additions & 17 deletions src/commands/settings/bad-word/EditBadWordCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ export default class EditBadWordCommand extends CompletingBadWordCommand {
}, {
name: 'Strike user',
value: 'strike'
}, {
name: 'Send direct message',
value: 'DM'
}
)
);
Expand Down Expand Up @@ -186,13 +183,26 @@ export default class EditBadWordCommand extends CompletingBadWordCommand {
new TextInputBuilder()
.setRequired(false)
.setCustomId('priority')
.setStyle(TextInputStyle.Paragraph)
.setStyle(TextInputStyle.Short)
.setPlaceholder('0')
.setLabel('Priority')
.setValue(badWord.priority.toString())
.setMinLength(1)
.setMaxLength(10)
)
),
/** @type {*} */
new ActionRowBuilder()
.addComponents(
/** @type {*} */
new TextInputBuilder()
.setRequired(false)
.setCustomId('dm')
.setStyle(TextInputStyle.Paragraph)
.setPlaceholder('This is a direct message sent to the user when their message was deleted')
.setLabel('Direct Message')
.setMinLength(1)
.setMaxLength(3000)
),
);

if (['ban', 'mute'].includes(punishment)) {
Expand Down Expand Up @@ -234,20 +244,25 @@ export default class EditBadWordCommand extends CompletingBadWordCommand {
return;
}

let trigger, response, duration = null, priority;
let trigger, response, duration = null, priority = null, dm = null;
for (let component of interaction.components) {
component = component.components[0];
if (component.customId === 'trigger') {
trigger = component.value;
}
else if (component.customId === 'response') {
response = component.value;
}
else if (component.customId === 'duration') {
duration = parseTime(component.value) || null;
}
else if (component.customId === 'priority') {
priority = parseInt(component.value) || 0;
switch (component.customId) {
case 'trigger':
trigger = component.value;
break;
case 'response':
response = component.value?.substring?.(0, 4000);
break;
case 'duration':
duration = parseTime(component.value) || null;
break;
case 'priority':
priority = parseInt(component.value) || 0;
break;
case 'dm':
dm = component.value?.substring?.(0, 3000);
break;
}
}

Expand All @@ -264,6 +279,7 @@ export default class EditBadWordCommand extends CompletingBadWordCommand {
confirmation.data.punishment,
duration,
priority,
dm,
confirmation.data.vision,
);
} else {
Expand Down Expand Up @@ -331,6 +347,7 @@ export default class EditBadWordCommand extends CompletingBadWordCommand {
* @param {PunishmentAction} punishment
* @param {?number} duration
* @param {number} priority
* @param {?string} dm
* @param {?boolean} vision
* @return {Promise<*>}
*/
Expand All @@ -345,6 +362,7 @@ export default class EditBadWordCommand extends CompletingBadWordCommand {
punishment,
duration,
priority,
dm,
vision,
) {
const badWord = /** @type {?BadWord} */
Expand All @@ -364,6 +382,7 @@ export default class EditBadWordCommand extends CompletingBadWordCommand {
}
badWord.trigger = triggerResponse.trigger;
badWord.response = response || 'disabled';
badWord.dm = dm || 'disabled';
badWord.punishment = new Punishment({action: punishment, duration});
badWord.priority = priority;
await badWord.save();
Expand Down
Loading

0 comments on commit d29acc8

Please sign in to comment.