Add SFW channels and link skip channels.

This commit is contained in:
Tarrgon
2025-06-04 22:52:08 -04:00
parent 11e3b3095a
commit f75b39484f
5 changed files with 144 additions and 23 deletions
+74 -2
View File
@@ -69,6 +69,30 @@ export default {
.setName('remove-staff-category') .setName('remove-staff-category')
.setDescription('Remove a category from staff categories.') .setDescription('Remove a category from staff categories.')
.setRequired(false) .setRequired(false)
)
.addChannelOption(option =>
option
.setName('add-safe-channel')
.setDescription('Add a SFW channel.')
.setRequired(false)
)
.addChannelOption(option =>
option
.setName('remove-safe-channel')
.setDescription('Remove a SFW channel.')
.setRequired(false)
)
.addChannelOption(option =>
option
.setName('add-link-skip-channel')
.setDescription('Add a link skip channel.')
.setRequired(false)
)
.addChannelOption(option =>
option
.setName('remove-link-skip-channel')
.setDescription('Remove a link skip channel.')
.setRequired(false)
), ),
handler: async function (client: Client, interaction: ChatInputCommandInteraction) { handler: async function (client: Client, interaction: ChatInputCommandInteraction) {
await interaction.deferReply({ flags: [MessageFlags.Ephemeral] }); await interaction.deferReply({ flags: [MessageFlags.Ephemeral] });
@@ -148,7 +172,7 @@ export default {
const addCategory = interaction.options.getChannel('add-staff-category'); const addCategory = interaction.options.getChannel('add-staff-category');
if (addCategory) { if (addCategory) {
if (addCategory.type == ChannelType.GuildCategory) { if (addCategory.type == ChannelType.GuildCategory) {
await Database.putGuildStaffCategory(interaction.guildId!, addCategory.id); await Database.putGuildArraySetting('staff_categories', interaction.guildId!, addCategory.id);
response += `Added ${addCategory.toString()} as a staff category.\n`; response += `Added ${addCategory.toString()} as a staff category.\n`;
} else { } else {
@@ -159,7 +183,7 @@ export default {
const removeCategory = interaction.options.getChannel('remove-staff-category'); const removeCategory = interaction.options.getChannel('remove-staff-category');
if (removeCategory) { if (removeCategory) {
if (removeCategory.type == ChannelType.GuildCategory) { if (removeCategory.type == ChannelType.GuildCategory) {
if (await Database.removeGuildStaffCategory(interaction.guildId!, removeCategory.id)) { if (await Database.removeGuildArraySetting('staff_categories', interaction.guildId!, removeCategory.id)) {
response += `Removed ${removeCategory.toString()} as a staff category\n`; response += `Removed ${removeCategory.toString()} as a staff category\n`;
} else { } else {
response += `Error removing staff category: ${removeCategory.toString()} isn't a staff category.`; response += `Error removing staff category: ${removeCategory.toString()} isn't a staff category.`;
@@ -169,6 +193,54 @@ export default {
} }
} }
const addSafeChannel = interaction.options.getChannel('add-safe-channel');
if (addSafeChannel) {
if (addSafeChannel.type == ChannelType.GuildText) {
await Database.putGuildArraySetting('safe_channels', interaction.guildId!, addSafeChannel.id);
response += `Added ${addSafeChannel.toString()} as a SFW cannel.\n`;
} else {
response += `Error adding SFW channel: ${addSafeChannel.toString()} isn't a text channel.`;
}
}
const removeSafeChannel = interaction.options.getChannel('remove-safe-channel');
if (removeSafeChannel) {
if (removeSafeChannel.type == ChannelType.GuildText) {
if (await Database.removeGuildArraySetting('safe_channels', interaction.guildId!, removeSafeChannel.id)) {
response += `Removed ${removeSafeChannel.toString()} as a safe channel\n`;
} else {
response += `Error removing safe channel: ${removeSafeChannel.toString()} isn't a safe channel.`;
}
} else {
response += `Error removing safe channel: ${removeSafeChannel.toString()} isn't a text channel.`;
}
}
const addLinkSkipChannel = interaction.options.getChannel('add-link-skip-channel');
if (addLinkSkipChannel) {
if (addLinkSkipChannel.type == ChannelType.GuildText) {
await Database.putGuildArraySetting('link_skip_channels', interaction.guildId!, addLinkSkipChannel.id);
response += `Added ${addLinkSkipChannel.toString()} as a link skip channel.\n`;
} else {
response += `Error adding link skip channel: ${addLinkSkipChannel.toString()} isn't a text channel.`;
}
}
const removeLinkSkipChannel = interaction.options.getChannel('remove-link-skip-channel');
if (removeLinkSkipChannel) {
if (removeLinkSkipChannel.type == ChannelType.GuildText) {
if (await Database.removeGuildArraySetting('link_skip_channels', interaction.guildId!, removeLinkSkipChannel.id)) {
response += `Removed ${removeLinkSkipChannel.toString()} as a staff category\n`;
} else {
response += `Error removing link skip channel: ${removeLinkSkipChannel.toString()} isn't a link skip channel.`;
}
} else {
response += `Error removing link skip channel: ${removeLinkSkipChannel.toString()} isn't a text channel.`;
}
}
if (response.length == 0) return interaction.editReply({ content: 'No settings provided.' }); if (response.length == 0) return interaction.editReply({ content: 'No settings provided.' });
interaction.editReply({ content: response }); interaction.editReply({ content: response });
+28 -3
View File
@@ -1,11 +1,11 @@
import { AllowedMentionsTypes, Message as DiscordMessage, GuildTextBasedChannel, OmitPartialGroupDMChannel, PartialMessage, ReadonlyCollection } from 'discord.js'; import { AllowedMentionsTypes, Message as DiscordMessage, GuildBasedChannel, GuildTextBasedChannel, OmitPartialGroupDMChannel, PartialMessage, ReadonlyCollection } from 'discord.js';
import { config } from '../config'; import { config } from '../config';
import { E621Post } from '../types'; import { E621Post } from '../types';
import { getE621Post, getE621PostByMd5, getPostUrl, hasBlacklistedTags } from '../utils/e621-utils'; import { getE621Post, getE621PostByMd5, getPostUrl, hasBlacklistedTags } from '../utils/e621-utils';
import { Database } from '../shared/Database'; import { Database } from '../shared/Database';
import { logDeletion, logEdit } from '../utils/message-logger'; import { logDeletion, logEdit } from '../utils/message-logger';
import { isEdited } from '../utils/message-utils'; import { isEdited } from '../utils/message-utils';
import { blipIDRegex, channelIsInStaffCategory, commentIDRegex, forumTopicIDRegex, poolIDRegex, postIDRegex, recordIDRegex, searchLinkRegex, setIDRegex, takedownIDRegex, ticketIDRegex, userIDRegex, wikiLinkRegex } from '../utils'; import { blipIDRegex, channelIgnoresLinks, channelIsInStaffCategory, channelIsSafe, commentIDRegex, forumTopicIDRegex, poolIDRegex, postIDRegex, recordIDRegex, searchLinkRegex, setIDRegex, takedownIDRegex, ticketIDRegex, userIDRegex, wikiLinkRegex } from '../utils';
export type Message<InGuild extends boolean = boolean> = OmitPartialGroupDMChannel<DiscordMessage<InGuild>>; export type Message<InGuild extends boolean = boolean> = OmitPartialGroupDMChannel<DiscordMessage<InGuild>>;
export type Partial = OmitPartialGroupDMChannel<PartialMessage>; export type Partial = OmitPartialGroupDMChannel<PartialMessage>;
@@ -166,6 +166,10 @@ export async function handleBulkMessageDelete(messages: ReadonlyCollection<strin
} }
async function searchHandler(message: Message, matchedGroups: RegExpExecArray[]): Promise<string | boolean> { async function searchHandler(message: Message, matchedGroups: RegExpExecArray[]): Promise<string | boolean> {
const skip = await channelIgnoresLinks(message.channel as GuildBasedChannel);
if (skip) return true;
let content = ''; let content = '';
for (const group of matchedGroups) { for (const group of matchedGroups) {
@@ -178,6 +182,10 @@ async function searchHandler(message: Message, matchedGroups: RegExpExecArray[])
} }
async function wikiPageHandler(message: Message, matchedGroups: RegExpExecArray[]): Promise<string | boolean> { async function wikiPageHandler(message: Message, matchedGroups: RegExpExecArray[]): Promise<string | boolean> {
const skip = await channelIgnoresLinks(message.channel as GuildBasedChannel);
if (skip) return true;
let content = ''; let content = '';
for (const group of matchedGroups) { for (const group of matchedGroups) {
@@ -241,7 +249,16 @@ async function postIdHandler(message: Message, matchedGroups: RegExpExecArray[])
if (await blacklistIfNecessary(message, posts)) return false; if (await blacklistIfNecessary(message, posts)) return false;
const content = posts.map(post => getPostUrl(post)).join('\n'); const skip = await channelIgnoresLinks(message.channel as GuildBasedChannel);
if (skip) return true;
const sfw = await channelIsSafe(message.channel as GuildBasedChannel);
const content = posts.map((post) => {
if (sfw && post.rating != 's') return ` [NSFW] <${getPostUrl(post)}>`;
return getPostUrl(post);
}).join('\n');
if (content.trim().length > 0) return content.trim(); if (content.trim().length > 0) return content.trim();
@@ -251,6 +268,10 @@ async function postIdHandler(message: Message, matchedGroups: RegExpExecArray[])
async function idHandler(path: string, message: Message, matchedGroups: RegExpExecArray[]): Promise<string | boolean> { async function idHandler(path: string, message: Message, matchedGroups: RegExpExecArray[]): Promise<string | boolean> {
if (!message.guildId) return true; if (!message.guildId) return true;
const skip = await channelIgnoresLinks(message.channel as GuildBasedChannel);
if (skip) return true;
const content = matchedGroups.map(m => `${config.E621_BASE_URL}/${path}/${m[1]}`).join('\n'); const content = matchedGroups.map(m => `${config.E621_BASE_URL}/${path}/${m[1]}`).join('\n');
if (content.trim().length > 0) return content.trim(); if (content.trim().length > 0) return content.trim();
@@ -293,6 +314,10 @@ async function imageHandler(message: Message, matchedGroups: RegExpExecArray[]):
if (await blacklistIfNecessary(message, posts)) return false; if (await blacklistIfNecessary(message, posts)) return false;
const skip = await channelIgnoresLinks(message.channel as GuildBasedChannel);
if (skip) return true;
const content = posts.map(post => `<${getPostUrl(post)}>`).join('\n'); const content = posts.map(post => `<${getPostUrl(post)}>`).join('\n');
if (content.trim().length > 0) return content.trim(); if (content.trim().length > 0) return content.trim();
+21 -17
View File
@@ -3,7 +3,7 @@ import { open, Database as SqliteDatabase } from 'sqlite';
import { config } from '../config'; import { config } from '../config';
import DiscordOAuth2 from 'discord-oauth2'; import DiscordOAuth2 from 'discord-oauth2';
import { serializeMessage, wait } from '../utils'; import { serializeMessage, wait } from '../utils';
import { GuildSettings, LoggedMessage, TicketMessage, TicketPhrase, Note, Ban } from '../types'; import { GuildSettings, LoggedMessage, TicketMessage, TicketPhrase, Note, Ban, GuildArraySetting } from '../types';
import { Message } from '../events'; import { Message } from '../events';
const DB_SCHEMA = ` const DB_SCHEMA = `
@@ -25,7 +25,9 @@ const DB_SCHEMA = `
voice_logs_channel_id TEXT, voice_logs_channel_id TEXT,
admin_role_id TEXT, admin_role_id TEXT,
private_help_role_id TEXT, private_help_role_id TEXT,
staff_categories TEXT staff_categories TEXT,
safe_channels TEXT,
link_skip_channels TEXT
); );
CREATE TABLE IF NOT EXISTS messages ( CREATE TABLE IF NOT EXISTS messages (
@@ -165,35 +167,37 @@ export class Database {
await Database.db.run('UPDATE settings SET private_help_role_id = ? WHERE guild_id = ?', id, guildId); await Database.db.run('UPDATE settings SET private_help_role_id = ? WHERE guild_id = ?', id, guildId);
} }
static async getGuildStaffCategories(guildId: string): Promise<string[]> { // Since "setting" has guaranteed values and is never set by the user, this shouldn't cause any security issues.
const settings = await Database.db.get<{ staff_categories: string }>('SELECT staff_categories FROM settings WHERE guild_id = ?', guildId); // But it does allow me to skip rewriting this a bunch.
static async getGuildArraySetting(setting: GuildArraySetting, guildId: string): Promise<string[]> {
const settings = await Database.db.get<{ [setting]: string }>(`SELECT ${setting} FROM settings WHERE guild_id = ?`, guildId);
if (!settings || !settings.staff_categories) return []; if (!settings || !settings[setting]) return [];
return settings.staff_categories.split(','); return settings[setting].split(',');
} }
static async putGuildStaffCategory(guildId: string, categoryId: string) { static async putGuildArraySetting(setting: GuildArraySetting, guildId: string, value: string) {
const categories = await Database.getGuildStaffCategories(guildId); const values = await Database.getGuildArraySetting(setting, guildId);
categories.push(categoryId); if (values.indexOf(value) == -1) values.push(value);
const newString = categories.join(','); const newString = values.join(',');
await Database.db.run('UPDATE settings SET staff_categories = ? WHERE guild_id = ?', newString, guildId); await Database.db.run(`UPDATE settings SET ${setting} = ? WHERE guild_id = ?`, newString, guildId);
} }
static async removeGuildStaffCategory(guildId: string, categoryId: string): Promise<boolean> { static async removeGuildArraySetting(setting: GuildArraySetting, guildId: string, value: string): Promise<boolean> {
const categories = await Database.getGuildStaffCategories(guildId); const values = await Database.getGuildArraySetting(setting, guildId);
const index = categories.indexOf(categoryId); const index = values.indexOf(value);
if (index == -1) return false; if (index == -1) return false;
categories.splice(index, 1); values.splice(index, 1);
const newString = categories.join(','); const newString = values.join(',');
await Database.db.run('UPDATE settings SET staff_categories = ? WHERE guild_id = ?', newString, guildId); await Database.db.run(`UPDATE settings SET ${setting} = ? WHERE guild_id = ?`, newString, guildId);
return true; return true;
} }
+4
View File
@@ -19,8 +19,12 @@ export type GuildSettings = {
admin_role_id?: string admin_role_id?: string
private_help_role_id?: string private_help_role_id?: string
staff_categories?: string staff_categories?: string
safe_channels?: string
link_skip_channels?: string
} }
export type GuildArraySetting = 'staff_categories' | 'safe_channels' | 'link_skip_channels';
export type TicketMessage = { export type TicketMessage = {
id: number id: number
message_id: string message_id: string
+17 -1
View File
@@ -4,9 +4,25 @@ import { Database } from '../shared/Database';
export async function channelIsInStaffCategory(channel: GuildBasedChannel) { export async function channelIsInStaffCategory(channel: GuildBasedChannel) {
if (!channel.guildId || !channel.parentId) return false; if (!channel.guildId || !channel.parentId) return false;
const staffCategories = await Database.getGuildStaffCategories(channel.guildId); const staffCategories = await Database.getGuildArraySetting('staff_categories', channel.guildId);
const parentChannel = await channel.guild.channels.fetch(channel.parentId); const parentChannel = await channel.guild.channels.fetch(channel.parentId);
return parentChannel?.parentId ? staffCategories.includes(parentChannel.parentId) : staffCategories.includes(channel.parentId); return parentChannel?.parentId ? staffCategories.includes(parentChannel.parentId) : staffCategories.includes(channel.parentId);
} }
export async function channelIsSafe(channel: GuildBasedChannel) {
if (!channel.guildId) return false;
const safeChannels = await Database.getGuildArraySetting('safe_channels', channel.guildId);
return safeChannels.includes(channel.id);
}
export async function channelIgnoresLinks(channel: GuildBasedChannel) {
if (!channel.guildId) return false;
const linkSkipChannels = await Database.getGuildArraySetting('link_skip_channels', channel.guildId);
return linkSkipChannels.includes(channel.id);
}