-
Notifications
You must be signed in to change notification settings - Fork 13
feat: add quoi-feur game with react #31
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
Changes from all commits
012896d
b4ac595
6879106
cbffe77
7486ef9
e065591
97a90fd
62bd548
6b7d6b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
# SETUP | ||
DISCORD_TOKEN= | ||
DISCORD_CLIENT_ID= | ||
DISCORD_GUILD_ID= | ||
COOL_LINKS_CHANNEL_ID= | ||
|
||
# DB | ||
REDIS_URL= | ||
PAGE_SUMMARIZER_BASE_URL= | ||
|
||
# CHANNELS | ||
BLABLA_CHANNEL_ID= | ||
COOL_LINKS_CHANNEL_ID= | ||
|
||
# API | ||
PAGE_SUMMARIZER_BASE_URL= |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
const socialNetworksUrlRegex = new RegExp( | ||
'^(https?://)?(www.)?(facebook.com|fb.me|twitter.com|vxtwitter.com|instagram.com|linkedin.com|youtube.com|youtu.be|pinterest.com|snapchat.com|tiktok.com)/[a-zA-Z0-9.-/?=&#_]+$', | ||
); | ||
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) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above - should be in module helpers. |
||
quoiDetectorRegex.test(removeEmoji(removePunctuation(text))); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import { coolLinksManagement } from './coolLinksManagement/coolLinksManagement.module'; | ||
import { fart } from './fart/fart.module'; | ||
import { patternReplace } from './patternReplace/patternReplace.module'; | ||
import { quoiFeur } from './quoiFeur/quoiFeur.module'; | ||
import { voiceOnDemand } from './voiceOnDemand/voiceOnDemand.module'; | ||
|
||
export const modules = { | ||
fart, | ||
voiceOnDemand, | ||
coolLinksManagement, | ||
patternReplace, | ||
quoiFeur, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { | ||
ChannelType, | ||
type ChatInputCommandInteraction, | ||
Guild, | ||
type Message, | ||
Role, | ||
} from 'discord.js'; | ||
|
||
import { cache } from '../../core/cache'; | ||
import { endWithQuoi } from '../../helpers/regex.helper'; | ||
|
||
const ONE_MINUTE = 1 * 60 * 1000; | ||
const MUTED_BY_BOT = 'Muted by bot'; | ||
|
||
const reactWithFeur = async (message: Message) => { | ||
await message.react('🇫'); | ||
await message.react('🇪'); | ||
await message.react('🇺'); | ||
await message.react('🇷'); | ||
}; | ||
|
||
const reactWithCoubeh = async (message: Message) => { | ||
await message.react('🇨'); | ||
await message.react('🇴'); | ||
await message.react('🇺'); | ||
await message.react('🇧'); | ||
await message.react('🇪'); | ||
await message.react('🇭'); | ||
await message.react('🔇'); | ||
|
||
const mutedRole = message.guild?.roles.cache.find((r) => r.name === MUTED_BY_BOT); | ||
|
||
if (!mutedRole?.id) return; | ||
|
||
await message.member?.roles.add(mutedRole.id); | ||
|
||
setTimeout(() => { | ||
message.member?.roles.remove(mutedRole.id).catch(console.error); | ||
}, ONE_MINUTE * 5); | ||
}; | ||
|
||
export const reactOnEndWithQuoi = async (message: Message) => { | ||
const channelIds = await cache.get('quoiFeurChannels', []); | ||
const channelHasGame = channelIds.find((channelId) => channelId === message.channelId); | ||
if (!channelHasGame) return; | ||
|
||
if (!endWithQuoi(message.content)) return; | ||
|
||
const probability = 1 / 20; | ||
|
||
try { | ||
Math.random() <= probability ? await reactWithCoubeh(message) : await reactWithFeur(message); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
}; | ||
|
||
export const createRoleMutedByBot = async (guild: Guild | null): Promise<Role> => { | ||
if (!guild) { | ||
throw new Error('Guild is null in createRoleMutedByBot'); | ||
} | ||
const existingMutedByBot = guild.roles.cache.find((role) => role.name === MUTED_BY_BOT); | ||
|
||
return ( | ||
existingMutedByBot ?? | ||
guild.roles.create({ | ||
name: MUTED_BY_BOT, | ||
}) | ||
); | ||
}; | ||
|
||
export const deleteRoleMutedByBot = async (guild: Guild | null): Promise<void> => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't depend on a specific guild but rather run for any guild the bot is in. So I recommend to remove the role for every guild the bot is in. I think you can change the signature of this and take a Client as input instead, so you can map it to the (or even code it inline if you want) |
||
if (!guild) { | ||
throw new Error('Guild is null in removeRoleMutedByBot'); | ||
} | ||
const existingMutedByBot = guild.roles.cache.find((role) => role.name === MUTED_BY_BOT); | ||
|
||
if (existingMutedByBot) { | ||
await existingMutedByBot.delete(); | ||
} | ||
}; | ||
|
||
export const addQuoiFeurToChannel = async (interaction: ChatInputCommandInteraction) => { | ||
const channel = interaction.channel; | ||
if (!channel || !channel.isTextBased() || channel.type !== ChannelType.GuildText) return; | ||
|
||
const channels = await cache.get('quoiFeurChannels', []); | ||
if (channels.includes(channel.id)) { | ||
await interaction.reply('Quoi-feur is already enabled in this channel'); | ||
return; | ||
} | ||
|
||
const role = await createRoleMutedByBot(interaction.guild); | ||
await channel.permissionOverwrites.create(role, { | ||
SendMessages: false, | ||
CreatePublicThreads: false, | ||
CreatePrivateThreads: false, | ||
SendMessagesInThreads: false, | ||
SendTTSMessages: false, | ||
AttachFiles: false, | ||
}); | ||
await cache.set('quoiFeurChannels', [...channels, channel.id]); | ||
await interaction.reply('Quoi-feur enabled in this channel'); | ||
}; | ||
|
||
export const removeQuoiFeurFromChannel = async (interaction: ChatInputCommandInteraction) => { | ||
const channel = interaction.channel; | ||
if (!channel || !channel.isTextBased() || channel.type !== ChannelType.GuildText) return; | ||
|
||
const channels = await cache.get('quoiFeurChannels', []); | ||
if (!channels.includes(channel.id)) { | ||
await interaction.reply('Quoi-feur is not enabled in this channel'); | ||
return; | ||
} | ||
|
||
const role = interaction.guild?.roles.cache.find((r) => r.name === MUTED_BY_BOT); | ||
if (role) { | ||
await channel.permissionOverwrites.delete(role); | ||
} | ||
await cache.set( | ||
'quoiFeurChannels', | ||
channels.filter((channelId) => channelId !== channel.id), | ||
); | ||
await interaction.reply('Quoi-feur disabled in this channel'); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { SlashCommandBuilder } from 'discord.js'; | ||
|
||
import { config } from '../../config'; | ||
import type { BotModule } from '../../types/bot'; | ||
import { | ||
addQuoiFeurToChannel, | ||
deleteRoleMutedByBot, | ||
reactOnEndWithQuoi, | ||
removeQuoiFeurFromChannel, | ||
} from './quoiFeur.helpers'; | ||
|
||
export const quoiFeur: BotModule = { | ||
slashCommands: [ | ||
{ | ||
schema: new SlashCommandBuilder() | ||
.setName('quoi-feur') | ||
.setDescription('Manage quoi-feur game in the channel') | ||
.addSubcommand((subcommand) => | ||
subcommand.setName('add').setDescription('Add the quoi-feur game to the channel'), | ||
) | ||
.addSubcommand((subcommand) => | ||
subcommand.setName('remove').setDescription('Remove the quoi-feur game from the channel'), | ||
) | ||
.toJSON(), | ||
handler: { | ||
add: async (interaction) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need to wrap this inside an arrow-function, you can pass directly Whenever a command throw/crash, it already prints out to the console as an error, so the .catch is unnecessary. Same with |
||
await addQuoiFeurToChannel(interaction).catch(console.error); | ||
}, | ||
remove: async (interaction) => { | ||
await removeQuoiFeurFromChannel(interaction).catch(console.error); | ||
}, | ||
}, | ||
}, | ||
], | ||
eventHandlers: { | ||
ready: async (client) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, you should pass directly |
||
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); | ||
}, | ||
}, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While
punctuationRegex
andemojiRegex
can make sense as global helpers, I don't think there's a lot of use-cases for other modules to detect sentences that ends withquoi
.Can you move this inside your module helper please ?