Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: issue with moderation plugin #64

Merged
merged 1 commit into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 4 additions & 40 deletions src/commands/moderation.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { SlashCommandBuilder } from '@discordjs/builders'
import { CommandInteraction } from 'discord.js'
import {
// enableSentimentAnalysis,
purgeMessages,
} from '../controllers/plugins/moderation.controller'
import { purgeMessages } from '../controllers/plugins/moderation.controller'
import { logger } from '../utils/debugging'

// https://discord.js.org/#/docs/main/stable/class/CommandInteraction?scrollTo=replied
//github.com/discordjs/discord.js/blob/main/packages/builders/docs/examples/Slash%20Command%20Builders.md
https: module.exports = {
module.exports = {
ephemeral: true,
data: new SlashCommandBuilder()
.setName('moderation')
Expand All @@ -27,48 +23,16 @@ https: module.exports = {
),
),
),
// .addSubcommandGroup((group) =>
// group
// .setName('sentiment')
// .setDescription('Plugin: Sentiment Analysis')
// .addSubcommand((subcommand) =>
// subcommand
// .setName('subscribe')
// .setDescription('Subscribe to sentiment analysis notifications')
// .addChannelOption((option) =>
// option
// .setRequired(true)
// .setName('channel')
// .setDescription('Channel for Hans to report'),
// ),
// )
// .addSubcommand((subcommand) =>
// subcommand
// .setName('toggle')
// .setDescription('Enables/Disables the plugin.')
// .addBooleanOption((option) =>
// option
// .setRequired(true)
// .setName('status')
// .setDescription('True for enable, False to disable'),
// ),
// ),
// ),
async execute(interaction: CommandInteraction) {
try {
if (!interaction.memberPermissions.has(['Administrator']))
return interaction.editReply({
content: 'You do not have permission to use this command',
})

if (!interaction.isChatInputCommand()) return

if (interaction.options.getSubcommandGroup() === 'sentiment') {
if (interaction.options.getSubcommand() === 'toggle') {
// await toggleSentimentAnalysis(interaction)
} else {
// await enableSentimentAnalysis(interaction)
}
} else if (interaction.options.getSubcommandGroup() === 'messages') {
if (interaction.options.getSubcommandGroup() === 'messages') {
await purgeMessages(interaction)
}
} catch (error) {
Expand Down
103 changes: 49 additions & 54 deletions src/controllers/plugins/moderation.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,55 @@ import { CommandInteraction, Message, TextChannel } from 'discord.js'
import { sentimentAnalysis } from '../../libs/sentiment'
import { sentimentUrgencyTable } from '../../utils/colors'

export const purgeMessages = async (interaction: CommandInteraction) => {
try {
const amount = interaction.options.get('n').value as number

if (!interaction.memberPermissions.has(['Administrator']))
return interaction.reply({
content: 'You do not have permission to use this command',
ephemeral: true,
})

if (amount > 100) {
return interaction.reply({
content: 'You can only delete up to 100 messages at once.',
ephemeral: true,
})
}

const fetched = await interaction.channel.messages.fetch({
limit: amount,
})

await interaction.channel.bulkDelete(fetched).catch((err) => {
throw Error(err.message)
})

await interaction.editReply({ content: `🗑 Deleted ${amount} messages.` })
} catch (error) {
throw Error(error.message)
}
}

export const removeLinks = async (
message: Message,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
allowedLinks: string[],
// eslint-disable-next-line @typescript-eslint/no-unused-vars
allowedRoles: string[],
) => {
if (message.member.permissions.has(['Administrator', 'DeafenMembers'])) return

const expression =
/(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})/gi
const regex = new RegExp(expression)

if (message.content.match(regex) !== null) {
await message.delete()
}
}

/**
* @param message Message
* @param notificationChannel DiscordJS TextChannel
Expand Down Expand Up @@ -61,57 +110,3 @@ export const sentimentAnalysisFn = async (
})
}
}

export const removeLinks = async (
message: Message,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
allowedLinks: string[],
// eslint-disable-next-line @typescript-eslint/no-unused-vars
allowedRoles: string[],
) => {
if (message.member.permissions.has(['Administrator', 'DeafenMembers'])) return

const expression =
/(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})/gi
const regex = new RegExp(expression)

if (message.content.match(regex) !== null) {
await message.delete()
}
}

export const purgeMessages = async (interaction: CommandInteraction) => {
try {
const amount = interaction.options.get('n').value as number

if (!interaction.memberPermissions.has(['Administrator']))
return interaction.reply({
content: 'You do not have permission to use this command',
ephemeral: true,
})

if (amount > 100) {
return interaction.reply({
content: 'You can only delete up to 100 messages at once.',
ephemeral: true,
})
}

const fetched = await interaction.channel.messages.fetch({
limit: amount,
})

await interaction.channel.bulkDelete(fetched).catch((err) => {
console.error(err)

return interaction.reply({
content: `💢 ${err}`,
ephemeral: true,
})
})

await interaction.reply({ content: `🗑 Deleted ${amount} messages.`, ephemeral: true })
} catch (error) {
interaction.reply(`Couldn't delete messages because of: ${error}`)
}
}