Skip to content

Commit

Permalink
Allow disabling message filter for a server
Browse files Browse the repository at this point in the history
  • Loading branch information
marvin-roesch committed Mar 12, 2023
1 parent 85ff65c commit 00832f9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/cmds/message-filter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const collections = guilds.view<FilterCollection>(
);
const exemptions = guilds.view<Array<MessageFilterExemption>>('messageFilter.exemptions', raw => raw.map(deserializeExemption));

function isEnabled(guildId: Snowflake) {
return guilds.get(guildId, 'messageFilter.enabled', v => v, true);
}

function buildEmbed (message: string, color?: ColorResolvable): EmbedBuilder {
const embed = new EmbedBuilder()
.setTitle('Message Filter')
Expand Down Expand Up @@ -57,6 +61,10 @@ async function filter (client: Client, message: Message | PartialMessage): Promi
return;
}

if (!isEnabled(message.guildId)) {
return;
}

const filterCollection = collections.get(message.guildId);
if (filterCollection === null || filterCollection.forbidden.length === 0) {
return;
Expand Down Expand Up @@ -297,6 +305,26 @@ export default {
command: new ComplexCommand(
'message-filter',
{
async state (client: Client, interaction: ChatInputCommandInteraction<'cached'>) {
const currentState = isEnabled(interaction.guildId);

if (currentState) {
await interaction.reply({ embeds: [buildEmbed('Message filtering is currently enabled for this server!')] });
} else {
await interaction.reply({ embeds: [buildEmbed('Message filtering is currently disabled for this server!')] });
}
},
async toggle (client: Client, interaction: ChatInputCommandInteraction<'cached'>) {
const currentState = isEnabled(interaction.guildId);
const newState = !currentState;
guilds.set(interaction.guildId, 'messageFilter.enabled', newState);

if (newState) {
await interaction.reply({ embeds: [buildEmbed('Message filtering is now enabled for this server!')] });
} else {
await interaction.reply({ embeds: [buildEmbed('Message filtering is now disabled for this server!')] });
}
},
forbidden: buildFilterManagement('forbidden'),
allowed: buildFilterManagement('allowed'),
exemptions: {
Expand Down
10 changes: 10 additions & 0 deletions src/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,16 @@ const cmds: ApplicationCommandDataResolvable[] = [
defaultMemberPermissions: PermissionFlagsBits.ManageMessages,
description: 'Manage message filters for this server',
options: [
{
type: ApplicationCommandOptionType.Subcommand,
name: 'state',
description: 'Check the state of message filtering for current server',
},
{
type: ApplicationCommandOptionType.Subcommand,
name: 'toggle',
description: 'En- or disable message filtering for current server',
},
{
type: ApplicationCommandOptionType.SubcommandGroup,
name: 'forbidden',
Expand Down

0 comments on commit 00832f9

Please sign in to comment.