-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,509 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
antirickroll | ||
urlfish | ||
urlfish | ||
archiver |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import type Context from "@framework/commands/Context"; | ||
import type { ContextReplyOptions } from "@framework/commands/Context"; | ||
import WizardButtonBuilder from "@framework/widgets/WizardButtonBuilder"; | ||
import type WizardManager from "@framework/widgets/WizardManager"; | ||
import type { ButtonInteraction } from "discord.js"; | ||
import { | ||
ActionRowBuilder, | ||
ButtonStyle, | ||
type AnyComponentBuilder, | ||
type Awaitable, | ||
type Message, | ||
type MessageEditOptions | ||
} from "discord.js"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
|
||
abstract class Wizard { | ||
private readonly manager: WizardManager; | ||
private readonly context: Context; | ||
private message: Message | null = null; | ||
private handlers: Record<string, string> = {}; | ||
private readonly id = uuidv4(); | ||
protected readonly inactivityTimeout: number = 300000; /* 5 minutes */ | ||
private timeout: Timer | null = null; | ||
protected readonly states: ContextReplyOptions[] = []; | ||
|
||
public constructor(manager: WizardManager, context: Context) { | ||
this.context = context; | ||
this.manager = manager; | ||
} | ||
|
||
protected button(customId: string): WizardButtonBuilder { | ||
return new WizardButtonBuilder() | ||
.setCustomId(`w::${this.id}::${customId}`) | ||
.setStyle(ButtonStyle.Secondary); | ||
} | ||
|
||
protected row<T extends AnyComponentBuilder>(components: T[]) { | ||
return new ActionRowBuilder<T>().addComponents(...components); | ||
} | ||
|
||
protected abstract render(): Awaitable<ContextReplyOptions>; | ||
|
||
public async update(): Promise<void> { | ||
const options = this.states.at(-1) ?? (await this.render()); | ||
|
||
this.handlers = {}; | ||
|
||
if (typeof options === "object" && "components" in options) { | ||
for (const component of options.components ?? []) { | ||
if (component instanceof ActionRowBuilder) { | ||
for (const button of component.components) { | ||
if (button instanceof WizardButtonBuilder) { | ||
this.handlers[button.customId] = button.handler; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (!this.message) { | ||
this.message = await this.context.reply(options); | ||
this.manager.register(this.id, this); | ||
this.timeout = setTimeout(() => this.dispose(), this.inactivityTimeout); | ||
} else { | ||
await this.message.edit(options as MessageEditOptions); | ||
} | ||
} | ||
|
||
protected pushState(options: ContextReplyOptions) { | ||
this.states.push(options); | ||
} | ||
|
||
protected popState() { | ||
return this.states.pop(); | ||
} | ||
|
||
protected async revertState(interaction?: ButtonInteraction) { | ||
const state = this.popState(); | ||
|
||
if (interaction) { | ||
await interaction.deferUpdate(); | ||
} | ||
|
||
if (state) { | ||
await this.update(); | ||
} | ||
|
||
return state; | ||
} | ||
|
||
public async dispatch(interaction: ButtonInteraction, customId: string) { | ||
const handler = this.handlers[customId]; | ||
|
||
if (this.timeout) { | ||
clearTimeout(this.timeout); | ||
} | ||
|
||
this.timeout = setTimeout(() => this.dispose(), this.inactivityTimeout); | ||
|
||
if (handler in this && typeof this[handler as keyof this] === "function") { | ||
const result = await ( | ||
this as unknown as Record< | ||
string, | ||
( | ||
interaction: ButtonInteraction, | ||
customId: string | ||
) => Awaitable<ContextReplyOptions> | ||
> | ||
)[handler].call(this, interaction, customId); | ||
|
||
if (result) { | ||
this.pushState(result); | ||
|
||
if (!interaction.deferred) { | ||
await interaction.deferUpdate(); | ||
} | ||
|
||
await interaction.message.edit(result as MessageEditOptions); | ||
} | ||
} | ||
} | ||
|
||
public dispose() { | ||
this.manager.dispose(this.id); | ||
} | ||
} | ||
|
||
export default Wizard; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { ButtonBuilder } from "discord.js"; | ||
|
||
class WizardButtonBuilder extends ButtonBuilder { | ||
private _customId?: string; | ||
private _handler?: string; | ||
|
||
public override setCustomId(customId: string): this { | ||
this._customId = customId; | ||
return super.setCustomId(customId); | ||
} | ||
|
||
public get customId(): string { | ||
return this._customId!; | ||
} | ||
|
||
public setHandler(handler: string): this { | ||
this._handler = handler; | ||
return this; | ||
} | ||
|
||
public get handler(): string { | ||
return this._handler!; | ||
} | ||
} | ||
|
||
export default WizardButtonBuilder; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { HasApplication } from "@framework/types/HasApplication"; | ||
import type Wizard from "@framework/widgets/Wizard"; | ||
|
||
class WizardManager extends HasApplication { | ||
private readonly wizards: Map<string, Wizard> = new Map(); | ||
|
||
public register(name: string, wizard: Wizard): void { | ||
this.wizards.set(name, wizard); | ||
} | ||
|
||
public get(name: string): Wizard | undefined { | ||
return this.wizards.get(name); | ||
} | ||
|
||
public dispose(name: string): void { | ||
this.wizards.delete(name); | ||
} | ||
} | ||
|
||
export default WizardManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* This file is part of SudoBot. | ||
* | ||
* Copyright (C) 2021, 2022, 2023, 2024 OSN Developers. | ||
* | ||
* SudoBot is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* SudoBot is distributed in the hope that it will be useful, but | ||
* WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with SudoBot. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { Command } from "@framework/commands/Command"; | ||
import { ContextType } from "@framework/commands/ContextType"; | ||
import type InteractionContext from "@framework/commands/InteractionContext"; | ||
import { PermissionFlags } from "@framework/permissions/PermissionFlag"; | ||
import type { ChatInputCommandInteraction, Guild } from "discord.js"; | ||
|
||
class SetupCommand extends Command<ContextType> { | ||
public override readonly name = "setup"; | ||
public override readonly description: string = "Setup the bot for this server."; | ||
public override readonly usage = [""]; | ||
public override readonly permissions = [PermissionFlags.ManageGuild]; | ||
public override readonly supportedContexts = [ContextType.ChatInput]; | ||
|
||
public override async execute(context: InteractionContext<ChatInputCommandInteraction>) { | ||
const { commandMessage } = context; | ||
|
||
if (!context.member || !commandMessage.guild) { | ||
await context.error("This command can only be used in a server."); | ||
return; | ||
} | ||
|
||
await this.application.service("guildSetupService").initialize( | ||
commandMessage as ChatInputCommandInteraction & { | ||
guild: Guild; | ||
}, | ||
context.user.id | ||
); | ||
} | ||
} | ||
|
||
export default SetupCommand; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.