add channel renderer + try to simplify adapter generics

This commit is contained in:
MapleLeaf
2021-12-27 19:22:21 -06:00
parent 6bfb1ab6de
commit 3682f67bfe
13 changed files with 143 additions and 43 deletions

View File

@@ -3,6 +3,7 @@
import { nanoid } from "nanoid"
import { raise } from "../helpers/raise"
import type { Adapter } from "./core/adapters/adapter"
import type { Channel } from "./internal/channel"
import type {
ButtonInteraction,
CommandInteraction,
@@ -16,9 +17,20 @@ import type {
MessageSelectOptions,
} from "./internal/message"
export class TestAdapter implements Adapter<TestCommandInteraction> {
type TestAdapterGenerics = {
commandReplyInit: TestCommandInteraction
channelInit: TestChannel
}
export class TestAdapter implements Adapter<TestAdapterGenerics> {
messages: TestMessage[] = []
private constructor() {}
static create(): Adapter<TestAdapterGenerics> & TestAdapter {
return new TestAdapter()
}
private componentInteractionListener: (
interaction: ComponentInteraction,
) => void = () => {}
@@ -35,6 +47,10 @@ export class TestAdapter implements Adapter<TestCommandInteraction> {
return interaction
}
createChannel(channel: TestChannel): Channel {
return channel
}
findButtonByLabel(label: string) {
for (const message of this.messages) {
for (const component of message.options.actionRows.flat()) {
@@ -162,3 +178,13 @@ export class TestSelectInteraction implements SelectInteraction {
this.message.options = options
}
}
export class TestChannel implements Channel {
constructor(private adapter: TestAdapter) {}
async send(messageOptions: MessageOptions): Promise<Message> {
const message = new TestMessage(messageOptions, this.adapter)
this.adapter.messages.push(message)
return message
}
}