-
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
Closed
Closed
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
012896d
feat: add quoi-feur game with react
luca-montaigut b4ac595
feat: remove role env var
luca-montaigut 6879106
feat: make quoi-feur stateless
luca-montaigut cbffe77
feat: add and remove game from channel
luca-montaigut 7486ef9
fix: avoid error if user delete message before react end
luca-montaigut e065591
fix: detail one minute constant
luca-montaigut 97a90fd
feat: improve quoi detection
luca-montaigut 62bd548
Merge branch 'master' into quoi-feur
luca-montaigut 6b7d6b0
refactor: match new architecture
luca-montaigut File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const MUTED_BY_BOT = 'Muted by bot'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { type Guild } from 'discord.js'; | ||
|
||
import { MUTED_BY_BOT } from '../constants/roles'; | ||
|
||
export const handleRoleCreation = async (guild: Guild) => { | ||
const hasMutedByBot = guild.roles.cache.find((role) => role.name === MUTED_BY_BOT); | ||
if (hasMutedByBot) { | ||
// delete to unmute all members and re-create it | ||
await hasMutedByBot.delete(); | ||
} | ||
await guild.roles.create({ | ||
name: MUTED_BY_BOT, | ||
}); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { type ChatInputCommandInteraction, type Message, TextChannel } from 'discord.js'; | ||
|
||
import { MUTED_BY_BOT } from './constants/roles'; | ||
import { cache } from './helpers/cache'; | ||
|
||
const quoiDetector = new RegExp(/\b\s*[qQ][uU][oO][iI]\s*[.,!?]*\s*$/i); | ||
const ONE_MINUTE = 60000; | ||
luca-montaigut marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const reactWithFeur = async (message: Message) => { | ||
await message.react('🇫'); | ||
neolectron marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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(() => { | ||
luca-montaigut marked this conversation as resolved.
Show resolved
Hide resolved
|
||
message.member?.roles.remove(mutedRole.id).catch(console.error); | ||
}, ONE_MINUTE * 5); | ||
}; | ||
|
||
export const quoiFeurReact = async (message: Message) => { | ||
const channelIds = await cache.get('quoiFeurChannels', []); | ||
const channelHasGame = channelIds.find((channelId) => channelId === message.channelId); | ||
if (!channelHasGame) return; | ||
|
||
if (!quoiDetector.test(message.content)) return; | ||
|
||
const probability = 1 / 20; | ||
|
||
try { | ||
Math.random() <= probability ? await reactWithCoubeh(message) : await reactWithFeur(message); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
}; | ||
|
||
export const addQuoiFeurChannel = async (interaction: ChatInputCommandInteraction) => { | ||
const channel = interaction.channel; | ||
if (!channel || !channel.isTextBased()) 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 = interaction.guild?.roles.cache.find((r) => r.name === MUTED_BY_BOT); | ||
if (!role) { | ||
throw new Error(`Role ${MUTED_BY_BOT} is missing`); | ||
} | ||
|
||
if (!(channel instanceof TextChannel)) return; | ||
luca-montaigut marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 removeQuoiFeurChannel = async (interaction: ChatInputCommandInteraction) => { | ||
const channel = interaction.channel; | ||
if (!channel || !channel.isTextBased()) 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) return; | ||
if (!(channel instanceof TextChannel)) return; | ||
|
||
await channel.permissionOverwrites.delete(role); | ||
await cache.set( | ||
'quoiFeurChannels', | ||
channels.filter((channelId) => channelId !== channel.id), | ||
); | ||
await interaction.reply('Quoi-feur disabled in this channel'); | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.