Skip to content

Commit

Permalink
favour .gen over do notation (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored Nov 5, 2024
1 parent 06eed5a commit d99e3fd
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 113 deletions.
95 changes: 47 additions & 48 deletions src/AutoThreads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,56 +62,55 @@ const make = Effect.gen(function* () {
.annotations({ identifier: "EligibleMessage" })
.pipe(Schema.decodeUnknown)

const handleMessages = gateway.handleDispatch("MESSAGE_CREATE", message =>
Effect.all({
message: EligibleMessage(message),
channel: channels
.get(message.guild_id!, message.channel_id)
.pipe(Effect.flatMap(EligibleChannel)),
}).pipe(
Effect.bind("title", () =>
ai.generateTitle(message.content).pipe(
Effect.tapErrorCause(Effect.log),
Effect.retry({
schedule: retryPolicy,
while: err => err._tag === "AiError",
}),
Effect.withSpan("AutoThreads.generateTitle"),
Effect.orElseSucceed(() =>
pipe(
Option.fromNullable(message.member?.nick),
Option.getOrElse(() => message.author.username),
name => `${name}'s thread`,
),
const handleMessages = gateway.handleDispatch("MESSAGE_CREATE", event =>
Effect.gen(function* () {
const message = yield* EligibleMessage(event)
const channel = yield* channels
.get(event.guild_id!, event.channel_id)
.pipe(Effect.flatMap(EligibleChannel))
const title = yield* ai.generateTitle(event.content).pipe(
Effect.tapErrorCause(Effect.log),
Effect.retry({
schedule: retryPolicy,
while: err => err._tag === "AiError",
}),
Effect.withSpan("AutoThreads.generateTitle"),
Effect.orElseSucceed(() =>
pipe(
Option.fromNullable(event.member?.nick),
Option.getOrElse(() => event.author.username),
name => `${name}'s thread`,
),
),
),
Effect.tap(({ title }) => Effect.annotateCurrentSpan({ title })),
Effect.bind(
"thread",
({ channel, title }) =>
rest.startThreadFromMessage(channel.id, message.id, {
name: Str.truncate(title, 100),
auto_archive_duration: 1440,
}).json,
),
Effect.tap(({ thread }) =>
rest.createMessage(thread.id, {
components: UI.grid([
[
UI.button({
custom_id: `edit_${message.author.id}`,
label: "Edit title",
}),
UI.button({
custom_id: `archive_${message.author.id}`,
label: "Archive",
style: Discord.ButtonStyle.SECONDARY,
}),
],
]),
}),
),
)

yield* Effect.annotateCurrentSpan({ title })

const thread = yield* rest.startThreadFromMessage(
channel.id,
message.id,
{
name: Str.truncate(title, 100),
auto_archive_duration: 1440,
},
).json

yield* rest.createMessage(thread.id, {
components: UI.grid([
[
UI.button({
custom_id: `edit_${event.author.id}`,
label: "Edit title",
}),
UI.button({
custom_id: `archive_${event.author.id}`,
label: "Archive",
style: Discord.ButtonStyle.SECONDARY,
}),
],
]),
})
}).pipe(
Effect.withSpan("AutoThreads.handleMessages"),
Effect.catchTag("ParseError", Effect.logDebug),
Effect.catchAllCause(Effect.logError),
Expand Down
81 changes: 37 additions & 44 deletions src/DocsLookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,30 +95,24 @@ const make = Effect.gen(function* () {
],
},
ix =>
Effect.all({
key: ix.optionValue("query"),
reveal: ix.optionValue("public"),
docs: allDocs,
Effect.gen(function* () {
const key = yield* ix.optionValue("query")
const reveal = yield* ix.optionValue("public")
const docs = yield* allDocs
const entry = yield* Effect.fromNullable(docs.map[key])
yield* Effect.annotateCurrentSpan({
entry: entry.signature,
public: reveal,
})
const embed = yield* entry.embed
return Ix.response({
type: Discord.InteractionCallbackType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
flags: reveal ? undefined : Discord.MessageFlag.EPHEMERAL,
embeds: [embed],
},
})
}).pipe(
Effect.bind("entry", ({ key, docs }) =>
Effect.fromNullable(docs.map[key]),
),
Effect.tap(({ entry, reveal }) =>
Effect.annotateCurrentSpan({
entry: entry.signature,
public: reveal,
}),
),
Effect.bind("embed", ({ entry }) => entry.embed),
Effect.map(({ embed, reveal }) =>
Ix.response({
type: Discord.InteractionCallbackType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
flags: reveal ? undefined : Discord.MessageFlag.EPHEMERAL,
embeds: [embed],
},
}),
),
Effect.catchTags({
NoSuchElementException: () =>
Effect.succeed(
Expand All @@ -138,27 +132,26 @@ const make = Effect.gen(function* () {

const autocomplete = Ix.autocomplete(
Ix.option("docs", "query"),
Ix.focusedOptionValue.pipe(
Effect.tap(query => Effect.annotateCurrentSpan("query", query)),
Effect.filterOrFail(
_ => _.length >= 3,
_ => new QueryTooShort({ actual: _.length, min: 3 }),
),
Effect.flatMap(search),
Effect.map(results =>
Ix.response({
type: Discord.InteractionCallbackType
.APPLICATION_COMMAND_AUTOCOMPLETE_RESULT,
data: {
choices: results.slice(0, 25).map(
({ label, key }): Discord.ApplicationCommandOptionChoice => ({
name: label,
value: key,
}),
),
},
}),
),
Effect.gen(function* () {
const query = yield* Ix.focusedOptionValue
yield* Effect.annotateCurrentSpan("query", query)
if (query.length < 3) {
return yield* new QueryTooShort({ actual: query.length, min: 3 })
}
const results = yield* search(query)
return Ix.response({
type: Discord.InteractionCallbackType
.APPLICATION_COMMAND_AUTOCOMPLETE_RESULT,
data: {
choices: results.slice(0, 25).map(
({ label, key }): Discord.ApplicationCommandOptionChoice => ({
name: label,
value: key,
}),
),
},
})
}).pipe(
Effect.catchTags({
QueryTooShort: _ =>
Effect.succeed(
Expand Down
36 changes: 15 additions & 21 deletions src/NoEmbed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ChannelsCache } from "bot/ChannelsCache"
import { DiscordLive } from "bot/Discord"
import { Discord, DiscordREST } from "dfx"
import { DiscordGateway } from "dfx/gateway"
import { Config, Effect, Layer, Schedule, Schema, pipe } from "effect"
import { Config, Effect, Layer, Schedule, Schema } from "effect"
import { nestedConfigProvider } from "./utils/Config.js"

const make = Effect.gen(function* () {
Expand Down Expand Up @@ -64,27 +64,21 @@ const make = Effect.gen(function* () {
Schema.decodeUnknown,
)

const handleMessage = (message: Discord.MessageCreateEvent) =>
pipe(
Effect.Do,
Effect.bind("channel", () =>
getChannel(message.guild_id!, message.channel_id).pipe(
Effect.flatMap(EligibleChannel),
),
),
Effect.bind("message", () =>
(message.content
? Effect.succeed(message)
: rest.getChannelMessage(message.channel_id, message.id).json
).pipe(Effect.flatMap(EligibleMessage)),
),
Effect.flatMap(({ message }) =>
rest.editMessage(message.channel_id, message.id, {
flags: message.flags | Discord.MessageFlag.SUPPRESS_EMBEDS,
}),
),
const handleMessage = (event: Discord.MessageCreateEvent) =>
Effect.gen(function* () {
yield* getChannel(event.guild_id!, event.channel_id).pipe(
Effect.flatMap(EligibleChannel),
)
const message = event.content
? event
: yield* rest
.getChannelMessage(event.channel_id, event.id)
.json.pipe(Effect.flatMap(EligibleMessage))
yield* rest.editMessage(message.channel_id, message.id, {
flags: message.flags! | Discord.MessageFlag.SUPPRESS_EMBEDS,
})
}).pipe(
Effect.withSpan("NoEmbed.handleMessage"),
Effect.catchTag("ParseError", Effect.logDebug),
Effect.catchAllCause(Effect.logError),
)

Expand Down

0 comments on commit d99e3fd

Please sign in to comment.