|
| 1 | +import { DiscordGatewayLayer } from "@chat/discord/DiscordGateway" |
| 2 | +import assert from "assert" |
| 3 | +import { Discord, DiscordREST, Ix, UI } from "dfx" |
| 4 | +import { InteractionsRegistry } from "dfx/gateway" |
| 5 | +import { Array, Effect, Layer } from "effect" |
| 6 | +import { RolesCache } from "./RolesCache.ts" |
| 7 | + |
| 8 | +export const NotificationsLayer = Effect.gen(function*() { |
| 9 | + const ix = yield* InteractionsRegistry |
| 10 | + const rolesCache = yield* RolesCache |
| 11 | + const rest = yield* DiscordREST |
| 12 | + |
| 13 | + const notificationRoles = Effect.fnUntraced(function*(guildId: string) { |
| 14 | + const roles = yield* rolesCache.getForParent(guildId) |
| 15 | + return Array.fromIterable(roles.values()).filter((role) => |
| 16 | + role.name.startsWith("🔔 ") |
| 17 | + ) |
| 18 | + }) |
| 19 | + |
| 20 | + const message = Effect.fn("Notifications.message")( |
| 21 | + function*(ix: Discord.APIInteraction, userRoles?: Array<string>) { |
| 22 | + const guildId = ix.guild_id |
| 23 | + if (!guildId) { |
| 24 | + return UI.components([ |
| 25 | + UI.textDisplay("This command can only be used in a server.") |
| 26 | + ], { ephemeral: true }) |
| 27 | + } |
| 28 | + |
| 29 | + const roles = yield* notificationRoles(guildId) |
| 30 | + if (roles.length === 0) { |
| 31 | + return UI.components([ |
| 32 | + UI.textDisplay("No notification roles found in this server.") |
| 33 | + ], { ephemeral: true }) |
| 34 | + } |
| 35 | + |
| 36 | + userRoles = userRoles ?? ix.member!.roles |
| 37 | + |
| 38 | + return UI.components([ |
| 39 | + UI.textDisplay("Select the notifications you want to receive:"), |
| 40 | + UI.row([ |
| 41 | + UI.select({ |
| 42 | + custom_id: "notifications_role", |
| 43 | + placeholder: "No notifications selected", |
| 44 | + min_values: 0, |
| 45 | + max_values: roles.length, |
| 46 | + options: roles.map((role) => ({ |
| 47 | + label: role.name, |
| 48 | + value: role.id, |
| 49 | + default: userRoles.includes(role.id) |
| 50 | + })) |
| 51 | + }) |
| 52 | + ]) |
| 53 | + ], { ephemeral: true }) |
| 54 | + } |
| 55 | + ) |
| 56 | + |
| 57 | + const command = Ix.guild( |
| 58 | + { |
| 59 | + name: "notifications", |
| 60 | + description: "Choose which notifications you want to receive" |
| 61 | + }, |
| 62 | + Effect.fn("Notifications.command")(function*(ix) { |
| 63 | + return Ix.response({ |
| 64 | + type: 4, |
| 65 | + data: yield* message(ix.interaction) |
| 66 | + }) |
| 67 | + }) |
| 68 | + ) |
| 69 | + |
| 70 | + const select = Ix.messageComponent( |
| 71 | + Ix.id("notifications_role"), |
| 72 | + Effect.gen(function*() { |
| 73 | + const ix = yield* Ix.Interaction |
| 74 | + const data = yield* Ix.MessageComponentData |
| 75 | + assert(data.component_type === 3) |
| 76 | + const roles = yield* notificationRoles(ix.guild_id!) |
| 77 | + |
| 78 | + const userRoles = new Set(ix.member?.roles ?? []) |
| 79 | + for (const role of roles) { |
| 80 | + const currentlyHas = userRoles.has(role.id) |
| 81 | + const shouldHave = data.values.includes(role.id) |
| 82 | + if (currentlyHas === shouldHave) { |
| 83 | + continue |
| 84 | + } else if (shouldHave) { |
| 85 | + userRoles.add(role.id) |
| 86 | + yield* rest.addGuildMemberRole( |
| 87 | + ix.guild_id!, |
| 88 | + ix.member!.user.id, |
| 89 | + role.id |
| 90 | + ) |
| 91 | + } else { |
| 92 | + userRoles.delete(role.id) |
| 93 | + yield* rest.deleteGuildMemberRole( |
| 94 | + ix.guild_id!, |
| 95 | + ix.member!.user.id, |
| 96 | + role.id |
| 97 | + ) |
| 98 | + } |
| 99 | + } |
| 100 | + return Ix.response({ |
| 101 | + type: Discord.InteractionCallbackTypes.UPDATE_MESSAGE, |
| 102 | + data: yield* message(ix, Array.fromIterable(userRoles)) |
| 103 | + }) |
| 104 | + }) |
| 105 | + ) |
| 106 | + |
| 107 | + yield* ix.register( |
| 108 | + Ix.builder.add(command).add(select).catchAll(Effect.logError) |
| 109 | + ) |
| 110 | +}).pipe( |
| 111 | + Layer.scopedDiscard, |
| 112 | + Layer.provide([RolesCache.Default, DiscordGatewayLayer]) |
| 113 | +) |
0 commit comments