Skip to content

Commit

Permalink
fix: pr returns adjustement
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-montaigut committed Sep 4, 2023
1 parent d9cb95a commit 92d14d9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 26 deletions.
3 changes: 0 additions & 3 deletions src/helpers/regex.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ const socialNetworksUrlRegex = new RegExp(
);
const punctuationRegex = new RegExp(/[.,!?]/g);
const emojiRegex = new RegExp(/(\p{Extended_Pictographic}|\p{Emoji_Component})/gu);
const quoiDetectorRegex = new RegExp(/\b\s*[q][u][o][i]\s*$/i);

export const isASocialNetworkUrl = (url: string): boolean => {
return socialNetworksUrlRegex.test(url);
};

export const removePunctuation = (text: string) => text.replaceAll(punctuationRegex, '');
export const removeEmoji = (text: string) => text.replaceAll(emojiRegex, '');
export const endWithQuoi = (text: string) =>
quoiDetectorRegex.test(removeEmoji(removePunctuation(text)));
21 changes: 13 additions & 8 deletions src/modules/quoiFeur/quoiFeur.helpers.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import {
ChannelType,
type ChatInputCommandInteraction,
Client,
Guild,
type Message,
Role,
} from 'discord.js';

import { cache } from '../../core/cache';
import { endWithQuoi } from '../../helpers/regex.helper';
import { removeEmoji, removePunctuation } from '../../helpers/regex.helper';

const ONE_MINUTE = 1 * 60 * 1000;
const MUTED_BY_BOT = 'Muted by bot';

const quoiDetectorRegex = new RegExp(/\b\s*[q][u][o][i]\s*$/i);
const endWithQuoi = (text: string) => quoiDetectorRegex.test(removeEmoji(removePunctuation(text)));

const reactWithFeur = async (message: Message) => {
await message.react('🇫');
await message.react('🇪');
Expand Down Expand Up @@ -69,14 +73,15 @@ export const createRoleMutedByBot = async (guild: Guild | null): Promise<Role> =
);
};

export const deleteRoleMutedByBot = async (guild: Guild | null): Promise<void> => {
if (!guild) {
throw new Error('Guild is null in removeRoleMutedByBot');
}
const existingMutedByBot = guild.roles.cache.find((role) => role.name === MUTED_BY_BOT);
export const deleteRoleMutedByBot = async (client: Client<true>): Promise<void> => {
const guilds = await client.guilds.fetch().then((guilds) => guilds.map((guild) => guild.fetch()));
const roles = await Promise.all(guilds).then((guilds) =>
guilds.map((guild) => guild.roles.cache.find((role) => role.name === MUTED_BY_BOT)),
);

if (existingMutedByBot) {
await existingMutedByBot.delete();
for (const role of roles) {
if (!role) continue;
await role.delete();
}
};

Expand Down
20 changes: 5 additions & 15 deletions src/modules/quoiFeur/quoiFeur.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { SlashCommandBuilder } from 'discord.js';

import { config } from '../../config';
import type { BotModule } from '../../types/bot';
import {
addQuoiFeurToChannel,
Expand All @@ -23,23 +22,14 @@ export const quoiFeur: BotModule = {
)
.toJSON(),
handler: {
add: async (interaction) => {
await addQuoiFeurToChannel(interaction).catch(console.error);
},
remove: async (interaction) => {
await removeQuoiFeurFromChannel(interaction).catch(console.error);
},
add: addQuoiFeurToChannel,
remove: removeQuoiFeurFromChannel,
},
},
],
eventHandlers: {
ready: async (client) => {
const guild = client.guilds.cache.get(config.discord.guildId) ?? null;
// unmute everyone on bot restart
await deleteRoleMutedByBot(guild).catch(console.error);
},
messageCreate: async (message) => {
await reactOnEndWithQuoi(message).catch(console.error);
},
// unmute everyone in every server on bot restart
ready: deleteRoleMutedByBot,
messageCreate: reactOnEndWithQuoi,
},
};

0 comments on commit 92d14d9

Please sign in to comment.