add channel renderer + try to simplify adapter generics
This commit is contained in:
13
library/internal/channel-message-renderer.ts
Normal file
13
library/internal/channel-message-renderer.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import type { Channel } from "./channel"
|
||||
import type { Message, MessageOptions } from "./message"
|
||||
import { Renderer } from "./renderer"
|
||||
|
||||
export class ChannelMessageRenderer extends Renderer {
|
||||
constructor(private channel: Channel) {
|
||||
super()
|
||||
}
|
||||
|
||||
protected createMessage(options: MessageOptions): Promise<Message> {
|
||||
return this.channel.send(options)
|
||||
}
|
||||
}
|
||||
5
library/internal/channel.ts
Normal file
5
library/internal/channel.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import type { Message, MessageOptions } from "./message"
|
||||
|
||||
export type Channel = {
|
||||
send(message: MessageOptions): Promise<Message>
|
||||
}
|
||||
22
library/internal/command-reply-renderer.ts
Normal file
22
library/internal/command-reply-renderer.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import type { CommandInteraction } from "./interaction"
|
||||
import type { Message, MessageOptions } from "./message"
|
||||
import { Renderer } from "./renderer"
|
||||
|
||||
// keep track of interaction ids which have replies,
|
||||
// so we know whether to call reply() or followUp()
|
||||
const repliedInteractionIds = new Set<string>()
|
||||
|
||||
export class CommandReplyRenderer extends Renderer {
|
||||
constructor(private interaction: CommandInteraction) {
|
||||
super()
|
||||
}
|
||||
|
||||
protected createMessage(options: MessageOptions): Promise<Message> {
|
||||
if (repliedInteractionIds.has(this.interaction.id)) {
|
||||
return this.interaction.followUp(options)
|
||||
}
|
||||
|
||||
repliedInteractionIds.add(this.interaction.id)
|
||||
return this.interaction.reply(options)
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import type { HostConfig } from "react-reconciler"
|
||||
import ReactReconciler from "react-reconciler"
|
||||
import { raise } from "../../helpers/raise.js"
|
||||
import { Node } from "./node.js"
|
||||
import type { Renderer } from "./renderer.js"
|
||||
import type { Renderer } from "./renderer"
|
||||
import { TextNode } from "./text-node.js"
|
||||
|
||||
const config: HostConfig<
|
||||
|
||||
@@ -1,32 +1,24 @@
|
||||
import type { Subscription } from "rxjs"
|
||||
import { Subject } from "rxjs"
|
||||
import { concatMap } from "rxjs/operators"
|
||||
import { Container } from "./container.js"
|
||||
import type { CommandInteraction, ComponentInteraction } from "./interaction"
|
||||
import type { ComponentInteraction } from "./interaction"
|
||||
import type { Message, MessageOptions } from "./message"
|
||||
import type { Node } from "./node.js"
|
||||
|
||||
// keep track of interaction ids which have replies,
|
||||
// so we know whether to call reply() or followUp()
|
||||
const repliedInteractionIds = new Set<string>()
|
||||
|
||||
type UpdatePayload =
|
||||
| { action: "update" | "deactivate"; options: MessageOptions }
|
||||
| { action: "destroy" }
|
||||
|
||||
export class Renderer {
|
||||
export abstract class Renderer {
|
||||
readonly nodes = new Container<Node<unknown>>()
|
||||
private componentInteraction?: ComponentInteraction
|
||||
private message?: Message
|
||||
private updates = new Subject<UpdatePayload>()
|
||||
private updateSubscription: Subscription
|
||||
private active = true
|
||||
private updates = new Subject<UpdatePayload>()
|
||||
|
||||
constructor(private interaction: CommandInteraction) {
|
||||
this.updateSubscription = this.updates
|
||||
.pipe(concatMap((payload) => this.updateMessage(payload)))
|
||||
.subscribe({ error: console.error })
|
||||
}
|
||||
private updateSubscription = this.updates
|
||||
.pipe(concatMap((payload) => this.updateMessage(payload)))
|
||||
.subscribe({ error: console.error })
|
||||
|
||||
render() {
|
||||
if (!this.active) {
|
||||
@@ -62,6 +54,8 @@ export class Renderer {
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract createMessage(options: MessageOptions): Promise<Message>
|
||||
|
||||
private getMessageOptions(): MessageOptions {
|
||||
const options: MessageOptions = {
|
||||
content: "",
|
||||
@@ -99,12 +93,6 @@ export class Renderer {
|
||||
return
|
||||
}
|
||||
|
||||
if (repliedInteractionIds.has(this.interaction.id)) {
|
||||
this.message = await this.interaction.followUp(payload.options)
|
||||
return
|
||||
}
|
||||
|
||||
repliedInteractionIds.add(this.interaction.id)
|
||||
this.message = await this.interaction.reply(payload.options)
|
||||
this.message = await this.createMessage(payload.options)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user