-
Notifications
You must be signed in to change notification settings - Fork 3
feat: archive channels #78
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
76081e1
feat: add ARCHIVED_CATEGORY_ID env variable
hmd-ali 7a32222
feat: add channel archiving
hmd-ali c3ccc9d
fix: add missing test env
hmd-ali c279b63
refactor: extract archive regex to a const and rename archive categor…
hmd-ali ed6f862
refactor: move archived channel startup check into archive feature
hmd-ali df96592
refactor: rename archive env variable
hmd-ali e411523
refactor: extract archive channels functions to a util file
hmd-ali 6739ff9
refactor: improve error handling in archive channels util
hmd-ali 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
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
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,38 @@ | ||
| import { createEvent } from '@/common/events/create-event.js'; | ||
| import { config } from '@/env.js'; | ||
| import { Events } from 'discord.js'; | ||
| import { | ||
| archiveChannel, | ||
| processingChannels, | ||
| unarchiveChannel, | ||
| } from './util.js'; | ||
|
|
||
| export default createEvent( | ||
| { | ||
| name: Events.ChannelUpdate, | ||
| }, | ||
| async (oldChannel, newChannel) => { | ||
| if (newChannel.isDMBased() || oldChannel.isDMBased()) { | ||
| return; | ||
| } | ||
|
|
||
| // We only care about channels that are being moved to or out of the archived category | ||
| if ( | ||
| newChannel.parentId !== config.channelIds.archiveCategory && | ||
| oldChannel.parentId !== config.channelIds.archiveCategory | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| // Prevent deadlocks from nested ChannelUpdate events caused by our own edits | ||
| if (processingChannels.has(newChannel.id)) { | ||
| return; | ||
| } | ||
|
|
||
| if (newChannel.parentId === config.channelIds.archiveCategory) { | ||
| await archiveChannel(newChannel); | ||
| } else { | ||
| await unarchiveChannel(newChannel); | ||
| } | ||
| } | ||
| ); |
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,129 @@ | ||
| import { config } from '@/env.js'; | ||
| import { | ||
| Guild, | ||
| ChannelType, | ||
| type GuildChannel, | ||
| PermissionFlagsBits, | ||
| } from 'discord.js'; | ||
|
|
||
| export const processingChannels = new Set<string>(); | ||
|
|
||
| export const ARCHIVED_REGEX = /^archived-/; | ||
|
|
||
| export const PUBLIC_PERMISSIONS = [ | ||
| PermissionFlagsBits.SendMessages, | ||
| PermissionFlagsBits.CreatePublicThreads, | ||
| PermissionFlagsBits.CreatePrivateThreads, | ||
| PermissionFlagsBits.SendMessagesInThreads, | ||
| PermissionFlagsBits.Connect, | ||
| ]; | ||
|
|
||
| export async function syncArchiveCategoryChannels(guild: Guild) { | ||
| const archiveCategory = guild.channels.cache.get( | ||
| config.channelIds.archiveCategory | ||
| ); | ||
|
|
||
| if (archiveCategory?.type !== ChannelType.GuildCategory) { | ||
| throw new Error( | ||
| `Archive category with ID ${config.channelIds.archiveCategory} not found in the guild.` | ||
| ); | ||
| } | ||
|
|
||
| const archivedChannels = archiveCategory.children.cache; | ||
| const results = await Promise.allSettled( | ||
| archivedChannels.map(archiveChannel) | ||
| ); | ||
|
|
||
| const failedReasons = []; | ||
| for (const result of results) { | ||
| if (result.status === 'rejected') { | ||
| failedReasons.push(result.reason); | ||
| } | ||
| } | ||
|
|
||
| if (failedReasons.length > 0) { | ||
| const errorMessages = failedReasons | ||
| .map((reason) => reason.message || reason) | ||
| .join('; '); | ||
| console.error(`Failed to archive some channels: ${errorMessages}`); | ||
| throw new Error(`Failed to archive some channels: ${errorMessages}`); | ||
| } | ||
| } | ||
| export async function setArchivedPermissions( | ||
| channel: GuildChannel, | ||
| archived: boolean | ||
| ) { | ||
| await channel.permissionOverwrites.edit( | ||
| channel.guild.roles.everyone.id, | ||
| { | ||
| SendMessages: archived ? false : null, | ||
| CreatePublicThreads: archived ? false : null, | ||
| CreatePrivateThreads: archived ? false : null, | ||
| SendMessagesInThreads: archived ? false : null, | ||
| Connect: archived ? false : null, | ||
| }, | ||
| { | ||
| reason: `${archived ? 'Archiving' : 'Unarchiving'} channel ${channel.name}`, | ||
| } | ||
| ); | ||
| } | ||
| export function hasArchivedPermissions(channel: GuildChannel) { | ||
| const everyoneRole = channel.guild.roles.everyone; | ||
| const overwrite = channel.permissionOverwrites.cache.get(everyoneRole.id); | ||
|
|
||
| if (!overwrite) { | ||
| return false; | ||
| } | ||
|
|
||
| return PUBLIC_PERMISSIONS.every((permission) => | ||
| overwrite.deny.has(permission) | ||
| ); | ||
| } | ||
| export async function unarchiveChannel(channel: GuildChannel) { | ||
| const channelName = channel.name; | ||
|
|
||
| if (!channelName.match(ARCHIVED_REGEX)) { | ||
| return; | ||
| } | ||
|
|
||
| const newChannelName = channelName.replace(ARCHIVED_REGEX, ''); | ||
|
|
||
| processingChannels.add(channel.id); | ||
|
|
||
| try { | ||
| await setArchivedPermissions(channel, false); | ||
| await channel.setName(newChannelName); | ||
| } catch (error) { | ||
| console.error( | ||
| `Error unarchiving channel ${channelName}:`, | ||
| (error as Error).message | ||
| ); | ||
| } finally { | ||
| processingChannels.delete(channel.id); | ||
| } | ||
| } | ||
| export async function archiveChannel(channel: GuildChannel) { | ||
| const channelName = channel.name; | ||
|
|
||
| const archivedChannelName = channelName.match(ARCHIVED_REGEX) | ||
| ? channelName | ||
| : `archived-${channelName}`; | ||
|
|
||
| if (archivedChannelName === channelName && hasArchivedPermissions(channel)) { | ||
| return; | ||
| } | ||
|
|
||
| processingChannels.add(channel.id); | ||
|
|
||
| try { | ||
| const renamedChannel = await channel.setName(archivedChannelName); | ||
| await setArchivedPermissions(renamedChannel, true); | ||
| } catch (error) { | ||
| // console.error(`Error archiving channel ${channelName}:`, error); | ||
| throw new Error( | ||
| `Error archiving channel ${channelName}: ${(error as Error).message}` | ||
| ); | ||
| } finally { | ||
| processingChannels.delete(channel.id); | ||
| } | ||
| } |
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
Oops, something went wrong.
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.