diff --git a/src/commands/settings.ts b/src/commands/settings.ts index 287f489..a478f0b 100644 --- a/src/commands/settings.ts +++ b/src/commands/settings.ts @@ -69,6 +69,30 @@ export default { .setName('remove-staff-category') .setDescription('Remove a category from staff categories.') .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) { await interaction.deferReply({ flags: [MessageFlags.Ephemeral] }); @@ -148,7 +172,7 @@ export default { const addCategory = interaction.options.getChannel('add-staff-category'); if (addCategory) { 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`; } else { @@ -159,7 +183,7 @@ export default { const removeCategory = interaction.options.getChannel('remove-staff-category'); if (removeCategory) { 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`; } else { 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.' }); interaction.editReply({ content: response }); diff --git a/src/events/handle-message.ts b/src/events/handle-message.ts index 770c80e..0cd03b5 100644 --- a/src/events/handle-message.ts +++ b/src/events/handle-message.ts @@ -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 { E621Post } from '../types'; import { getE621Post, getE621PostByMd5, getPostUrl, hasBlacklistedTags } from '../utils/e621-utils'; import { Database } from '../shared/Database'; import { logDeletion, logEdit } from '../utils/message-logger'; 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 = OmitPartialGroupDMChannel>; export type Partial = OmitPartialGroupDMChannel; @@ -166,6 +166,10 @@ export async function handleBulkMessageDelete(messages: ReadonlyCollection { + const skip = await channelIgnoresLinks(message.channel as GuildBasedChannel); + + if (skip) return true; + let content = ''; for (const group of matchedGroups) { @@ -178,6 +182,10 @@ async function searchHandler(message: Message, matchedGroups: RegExpExecArray[]) } async function wikiPageHandler(message: Message, matchedGroups: RegExpExecArray[]): Promise { + const skip = await channelIgnoresLinks(message.channel as GuildBasedChannel); + + if (skip) return true; + let content = ''; for (const group of matchedGroups) { @@ -241,7 +249,16 @@ async function postIdHandler(message: Message, matchedGroups: RegExpExecArray[]) 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(); @@ -251,6 +268,10 @@ async function postIdHandler(message: Message, matchedGroups: RegExpExecArray[]) async function idHandler(path: string, message: Message, matchedGroups: RegExpExecArray[]): Promise { 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'); 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; + const skip = await channelIgnoresLinks(message.channel as GuildBasedChannel); + + if (skip) return true; + const content = posts.map(post => `<${getPostUrl(post)}>`).join('\n'); if (content.trim().length > 0) return content.trim(); diff --git a/src/shared/Database.ts b/src/shared/Database.ts index 9b4b5aa..0b63737 100644 --- a/src/shared/Database.ts +++ b/src/shared/Database.ts @@ -3,7 +3,7 @@ import { open, Database as SqliteDatabase } from 'sqlite'; import { config } from '../config'; import DiscordOAuth2 from 'discord-oauth2'; 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'; const DB_SCHEMA = ` @@ -25,7 +25,9 @@ const DB_SCHEMA = ` voice_logs_channel_id TEXT, admin_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 ( @@ -165,35 +167,37 @@ export class Database { await Database.db.run('UPDATE settings SET private_help_role_id = ? WHERE guild_id = ?', id, guildId); } - static async getGuildStaffCategories(guildId: string): Promise { - const settings = await Database.db.get<{ staff_categories: string }>('SELECT staff_categories FROM settings WHERE guild_id = ?', guildId); + // Since "setting" has guaranteed values and is never set by the user, this shouldn't cause any security issues. + // But it does allow me to skip rewriting this a bunch. + static async getGuildArraySetting(setting: GuildArraySetting, guildId: string): Promise { + 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) { - const categories = await Database.getGuildStaffCategories(guildId); + static async putGuildArraySetting(setting: GuildArraySetting, guildId: string, value: string) { + 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 { - const categories = await Database.getGuildStaffCategories(guildId); + static async removeGuildArraySetting(setting: GuildArraySetting, guildId: string, value: string): Promise { + const values = await Database.getGuildArraySetting(setting, guildId); - const index = categories.indexOf(categoryId); + const index = values.indexOf(value); 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; } diff --git a/src/types/database-types.d.ts b/src/types/database-types.d.ts index ab394dd..dc49d61 100644 --- a/src/types/database-types.d.ts +++ b/src/types/database-types.d.ts @@ -19,8 +19,12 @@ export type GuildSettings = { admin_role_id?: string private_help_role_id?: string staff_categories?: string + safe_channels?: string + link_skip_channels?: string } +export type GuildArraySetting = 'staff_categories' | 'safe_channels' | 'link_skip_channels'; + export type TicketMessage = { id: number message_id: string diff --git a/src/utils/channel-utils.ts b/src/utils/channel-utils.ts index 7aa4019..facdfcc 100644 --- a/src/utils/channel-utils.ts +++ b/src/utils/channel-utils.ts @@ -4,9 +4,25 @@ import { Database } from '../shared/Database'; export async function channelIsInStaffCategory(channel: GuildBasedChannel) { 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); 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); } \ No newline at end of file