Skip to content

Commit b4ac595

Browse files
feat: remove role env var
1 parent 012896d commit b4ac595

File tree

6 files changed

+46
-5
lines changed

6 files changed

+46
-5
lines changed

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ BLABLA_CHANNEL_ID=
1111
COOL_LINKS_CHANNEL_ID=
1212

1313
# API
14-
PAGE_SUMMARIZER_BASE_URL==
14+
PAGE_SUMMARIZER_BASE_URL=

src/config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export const config = {
77
guildId: env.get('DISCORD_GUILD_ID').required().asString(),
88
blablaChannelId: env.get('BLABLA_CHANNEL_ID').required().asString(),
99
coolLinksChannelId: env.get('COOL_LINKS_CHANNEL_ID').required().asString(),
10-
mutedRoleId: env.get('MUTED_ROLE_ID').required().asString(),
1110
},
1211
redis: {
1312
url: env.get('REDIS_URL').required().asString(),

src/constants.ts/roles.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const MUTED_BY_BOT = 'Muted by bot';

src/handlers/handle-role-creation.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { type Guild, TextChannel } from 'discord.js';
2+
3+
import { MUTED_BY_BOT } from '../constants.ts/roles';
4+
5+
const createMutedByBotRole = async (guild: Guild) => {
6+
const role = await guild.roles.create({
7+
name: MUTED_BY_BOT,
8+
});
9+
guild.channels.cache.forEach((channel) => {
10+
if (!(channel instanceof TextChannel)) return;
11+
channel.permissionOverwrites
12+
.create(role, {
13+
SendMessages: false,
14+
CreatePublicThreads: false,
15+
CreatePrivateThreads: false,
16+
SendMessagesInThreads: false,
17+
SendTTSMessages: false,
18+
AttachFiles: false,
19+
})
20+
.catch(console.error);
21+
});
22+
};
23+
24+
export const handleRoleCreation = async (guild: Guild) => {
25+
const hasMutedByBot = guild.roles.cache.find((role) => role.name === MUTED_BY_BOT);
26+
if (hasMutedByBot) {
27+
// delete to unmute all members and re-create it
28+
await hasMutedByBot.delete();
29+
}
30+
await createMutedByBotRole(guild);
31+
};

src/main.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { config } from './config';
55
import { deleteExistingCommands } from './delete-existing-commands';
66
import { handleGuildMessageCreation } from './handlers/handle-guild-message-creation';
77
import { handleInteractionCreation } from './handlers/handle-interaction-creation';
8+
import { handleRoleCreation } from './handlers/handle-role-creation';
89
import { handleVoiceChannelDeletion } from './handlers/handle-voice-channel-deletion';
910
import { handleVoiceStateUpdate } from './handlers/handle-voice-state-update';
1011

@@ -54,4 +55,7 @@ await rest.put(Routes.applicationGuildCommands(discord.clientId, discord.guildId
5455
body: [voiceOnDemandCommand, fartCommand],
5556
});
5657

58+
const guild = await client.guilds.fetch(discord.guildId);
59+
await handleRoleCreation(guild);
60+
5761
console.log('Bot started.');

src/quoi-feur.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Message } from 'discord.js';
2-
import { config } from './config';
2+
3+
import { MUTED_BY_BOT } from './constants.ts/roles';
34

45
const quoiDetector = new RegExp(/\b\s*[qQ][uU][oO][iI]\s*[.,!?]*\s*$/i);
56
const ONE_MINUTE = 60000;
@@ -20,9 +21,14 @@ const reactWithCoubeh = async (message: Message) => {
2021
await message.react('🇭');
2122
await message.react('🔇');
2223

23-
message.member?.roles.add(config.discord.mutedRoleId);
24+
const mutedRole = message.guild?.roles.cache.find((r) => r.name === MUTED_BY_BOT);
25+
26+
if (!mutedRole?.id) return;
27+
28+
await message.member?.roles.add(mutedRole.id);
29+
2430
setTimeout(() => {
25-
message.member?.roles.remove(config.discord.mutedRoleId);
31+
message.member?.roles.remove(mutedRole.id).catch(console.error);
2632
}, ONE_MINUTE * 5);
2733
};
2834

0 commit comments

Comments
 (0)