Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ Channels listed in [`src/config/channels.ts`](src/config/channels.ts) are manage
},
```

- **Thread-only channels are a channel type, not a setting.** Discord has no "require threads" switch; thread-only behaviour exists only as `forum` and `media` channels, and the API cannot convert a text channel into one. To make an existing channel thread-only, declare a new `forum`/`media` channel (without `id`) and retire the old channel by hand. Declaring `type: 'forum'` on the `id` of a text channel fails the deploy with an error saying so.
- **Thread-only channels are a channel type or a permission policy, not a setting.** Discord has no "require threads" switch. `forum` and `media` channels are thread-only by type, but the API cannot convert a text channel into one: declaring `type: 'forum'` on the `id` of a text channel fails the deploy with an error saying so. To make an existing text channel thread-only in place, keeping its ID and history, adopt it by `id` and declare `permissionOverwrites` that deny `SEND_MESSAGES` to `DISCORD_EVERYONE_ROLE` and allow `CREATE_PUBLIC_THREADS` and `SEND_MESSAGES_IN_THREADS`; members can then only start threads and reply in them. Add an overwrite allowing `SEND_MESSAGES` for any role that should still post at the top level (`#general` does this for moderators and community managers).
- **Permission overwrites are managed per permission.** Each `permissionOverwrites` entry names a role from `roles.ts` (or `DISCORD_EVERYONE_ROLE`) and the permissions to `allow` and `deny`, limited to the allowlist in [`discordPermissions.ts`](src/config/discordPermissions.ts). Pulumi owns only the listed permissions: other permissions on the same overwrite, and overwrites for other roles or members, stay as they are on Discord. Removing a permission or an entry clears just those bits on the next deploy. The bot needs **Manage Roles** to write overwrites, and Discord only lets it allow or deny permissions it holds itself. Administrators bypass overwrites.
- **Channels are never deleted.** Removing an entry only drops the resource from Pulumi state; the channel and its history stay on Discord. Changing an entry's `id` or `type` likewise adopts/creates the new channel and leaves the old one alone.
- Only declared settings are managed. `name` is required when creating a channel and optional when adopting one by `id`; declare it only if you want it enforced. Validation (`npm run check`) rejects a missing name on a new channel, names outside 1-100 characters, topics over Discord's limit (1024 for text, 4096 for forum/media), malformed snowflakes, duplicate entries, more than 20 tags, `requireTag` with no tags to pick from, and forum/media settings on text channels (`defaultForumLayout` is forum-only). It prints a `WARNING:` without failing for names with uppercase letters or spaces (Discord normalizes those, so the declared name would never match) and for `requireTag` on an adopted channel that declares no `availableTags` (it relies on the tags already on Discord; the deploy fails if there are none).
- `position` is applied on creation only. Discord renumbers positions whenever channels move, so it is not checked or re-applied afterwards; reorder channels in Discord.
Expand Down
146 changes: 146 additions & 0 deletions scripts/test-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,20 @@ import {
getDiscordChannelConfigErrors,
getDiscordChannelConfigWarnings,
getDiscordChannelDrift,
getDiscordPermissionOverwriteDrift,
planDiscordPermissionOverwrites,
resolveDiscordPermissionOverwrite,
DISCORD_EVERYONE_ROLE,
type DiscordChannelApiResponse,
type DiscordChannelConfig,
type DiscordNewChannelConfig,
} from '../src/config/channels';
import {
DISCORD_ROLE_PERMISSION_BITS,
discordChannelPermissionBitfield,
discordPermissionBitfield,
getDiscordRolePermissionErrors,
mergeDiscordPermissionOverwrite,
mergeDiscordRolePermissions,
} from '../src/config/discordPermissions';

Expand Down Expand Up @@ -678,6 +684,146 @@ test('Tag drift ignores Discord tag IDs and ordering but catches changed tags',
);
});

// Discord channel permission overwrites
console.log('\nTesting Discord channel permission overwrites...\n');
const SEND = (1n << 11n).toString(); // SEND_MESSAGES
const THREADS = ((1n << 35n) | (1n << 38n)).toString(); // CREATE_PUBLIC_THREADS | SEND_MESSAGES_IN_THREADS
const VIEW = (1n << 10n).toString(); // VIEW_CHANNEL, not managed
const THREAD_ONLY = resolveDiscordPermissionOverwrite(
{
role: DISCORD_EVERYONE_ROLE,
deny: ['SEND_MESSAGES'],
allow: ['CREATE_PUBLIC_THREADS', 'SEND_MESSAGES_IN_THREADS'],
},
OTHER_SNOWFLAKE
);
test('Channel permission bits match the Discord API', () =>
discordChannelPermissionBitfield(['SEND_MESSAGES']) === '2048' &&
discordChannelPermissionBitfield(['CREATE_PUBLIC_THREADS']) === '34359738368' &&
discordChannelPermissionBitfield(['CREATE_PRIVATE_THREADS']) === '68719476736' &&
discordChannelPermissionBitfield(['SEND_MESSAGES_IN_THREADS']) === '274877906944' &&
discordChannelPermissionBitfield([]) === '0');
test('An overwrite resolves to the role ID and allow/deny bitfields', () =>
THREAD_ONLY.id === OTHER_SNOWFLAKE && THREAD_ONLY.deny === SEND && THREAD_ONLY.allow === THREADS);
test('Overwrite merge keeps unmanaged bits and moves a bit declared on the other side', () => {
// VIEW_CHANNEL denied by hand stays; SEND_MESSAGES allowed by hand becomes denied
const live = { allow: SEND, deny: VIEW };
const merged = mergeDiscordPermissionOverwrite(live, undefined, { allow: THREADS, deny: SEND });
return merged.allow === THREADS && merged.deny === (BigInt(VIEW) | BigInt(SEND)).toString();
});
test('Overwrite merge clears bits that were declared before and no longer are', () => {
const live = { allow: THREADS, deny: (BigInt(VIEW) | BigInt(SEND)).toString() };
const merged = mergeDiscordPermissionOverwrite(live, THREAD_ONLY, undefined);
return merged.allow === '0' && merged.deny === VIEW;
});
test('Overwrite plan writes a missing overwrite and nothing once it is live', () => {
const plan = planDiscordPermissionOverwrites([], undefined, [THREAD_ONLY]);
const live = [{ ...THREAD_ONLY, type: 0 }];
return (
plan.length === 1 &&
plan[0].id === OTHER_SNOWFLAKE &&
plan[0].allow === THREADS &&
plan[0].deny === SEND &&
planDiscordPermissionOverwrites(live, [THREAD_ONLY], [THREAD_ONLY]).length === 0
);
});
test('Overwrite plan leaves overwrites that were never declared alone', () => {
const live = [
{ id: SNOWFLAKE, type: 1, allow: SEND, deny: '0' },
{ ...THREAD_ONLY, type: 0 },
];
return planDiscordPermissionOverwrites(live, [THREAD_ONLY], [THREAD_ONLY]).length === 0;
});
test('Overwrite plan clears an overwrite that was removed from config', () => {
const live = [{ id: OTHER_SNOWFLAKE, type: 0, allow: THREADS, deny: SEND }];
const plan = planDiscordPermissionOverwrites(live, [THREAD_ONLY], undefined);
return plan.length === 1 && plan[0].allow === '0' && plan[0].deny === '0';
});
test('Overwrite drift fires only when a declared permission is missing live', () => {
const drift = (permission_overwrites: DiscordChannelApiResponse['permission_overwrites']) => {
const state = discordChannelStateFromApi({ ...TEXT_API, permission_overwrites });
return getDiscordPermissionOverwriteDrift([THREAD_ONLY], state).join(',');
};
const live = (allow: string, deny: string) => [{ id: OTHER_SNOWFLAKE, type: 0, allow, deny }];
const withHandSetBits = (BigInt(VIEW) | BigInt(SEND)).toString();
const bare = discordChannelStateFromApi(TEXT_API);
return (
drift(live(THREADS, SEND)) === '' &&
// Extra bits set by hand are not drift
drift(live(THREADS, withHandSetBits)) === '' &&
drift(live(THREADS, '0')) === OTHER_SNOWFLAKE &&
drift(undefined) === OTHER_SNOWFLAKE &&
getDiscordPermissionOverwriteDrift(undefined, bare).length === 0
);
});
test('Channel payloads never contain permission overwrites', () => {
const config: DiscordNewChannelConfig = {
name: 'a',
type: 'text',
permissionOverwrites: [{ role: DISCORD_EVERYONE_ROLE, deny: ['SEND_MESSAGES'] }],
};
return (
!('permission_overwrites' in discordChannelPayloadFromConfig(config)) &&
!('permission_overwrites' in discordChannelCreatePayloadFromConfig(config))
);
});
test('Channel validation accepts thread-only overwrites on an adopted text channel', () =>
isClean([
{
id: SNOWFLAKE,
type: 'text',
permissionOverwrites: [
{
role: DISCORD_EVERYONE_ROLE,
deny: ['SEND_MESSAGES'],
allow: ['CREATE_PUBLIC_THREADS', 'SEND_MESSAGES_IN_THREADS'],
},
{ role: ROLE_IDS.MODERATORS, allow: ['SEND_MESSAGES'] },
],
},
]));
test('Channel validation rejects bad permission overwrites', () => {
const withOverwrites = (permissionOverwrites: unknown): DiscordChannelConfig[] => [
{ id: SNOWFLAKE, type: 'text', permissionOverwrites } as unknown as DiscordChannelConfig,
];
return (
hasErrorMatching(
withOverwrites([{ role: 'no-such-role', deny: ['SEND_MESSAGES'] }]),
/"no-such-role" names a role that is not in roles.ts or has no Discord role/
) &&
// GitHub-only role: no Discord role to attach the overwrite to
hasErrorMatching(
withOverwrites([{ role: ROLE_IDS.SECURITY_TEAM, deny: ['SEND_MESSAGES'] }]),
/has no Discord role/
) &&
hasErrorMatching(
withOverwrites([
{ role: DISCORD_EVERYONE_ROLE, deny: ['SEND_MESSAGES'] },
{ role: DISCORD_EVERYONE_ROLE, allow: ['CREATE_PUBLIC_THREADS'] },
]),
/"@everyone" is declared twice/
) &&
hasErrorMatching(
withOverwrites([{ role: DISCORD_EVERYONE_ROLE }]),
/allows and denies nothing/
) &&
hasErrorMatching(
withOverwrites([{ role: DISCORD_EVERYONE_ROLE, deny: ['ADMINISTRATOR'] }]),
/deny permission "ADMINISTRATOR" which is not allowed/
) &&
hasErrorMatching(
withOverwrites([{ role: DISCORD_EVERYONE_ROLE, allow: ['SEND_MESSAGES', 'SEND_MESSAGES'] }]),
/lists allow permission "SEND_MESSAGES" twice/
) &&
hasErrorMatching(
withOverwrites([
{ role: DISCORD_EVERYONE_ROLE, allow: ['SEND_MESSAGES'], deny: ['SEND_MESSAGES'] },
]),
/both allows and denies "SEND_MESSAGES"/
)
);
});

// Discord role permissions
console.log('\nTesting Discord role permissions...\n');
const EMOJI = (1n << 18n).toString(); // USE_EXTERNAL_EMOJIS
Expand Down
Loading
Loading