mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-03 05:26:07 +00:00
wip: CommandInteraction.respond
This commit is contained in:
parent
fbe3a6402c
commit
7203eb01eb
@ -48,6 +48,7 @@ export interface CreateMessage {
|
|||||||
allowedMentions?: AllowedMentions;
|
allowedMentions?: AllowedMentions;
|
||||||
files?: FileContent[];
|
files?: FileContent[];
|
||||||
messageReference?: CreateMessageReference;
|
messageReference?: CreateMessageReference;
|
||||||
|
tts?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -271,6 +272,7 @@ export class Message implements Model {
|
|||||||
}
|
}
|
||||||
: undefined,
|
: undefined,
|
||||||
embeds: options.embeds,
|
embeds: options.embeds,
|
||||||
|
tts: options.tts,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
import type { Model } from "./Base.ts";
|
import type { Model } from "./Base.ts";
|
||||||
import type { Session } from "../session/Session.ts";
|
import type { Session } from "../session/Session.ts";
|
||||||
import type { Snowflake } from "../util/Snowflake.ts";
|
import type { Snowflake } from "../util/Snowflake.ts";
|
||||||
import type { DiscordWebhook, WebhookTypes } from "../vendor/external.ts";
|
import type { DiscordMessage, DiscordWebhook, WebhookTypes } from "../vendor/external.ts";
|
||||||
|
import type { WebhookOptions } from "../util/Routes.ts";
|
||||||
|
import type { CreateMessage } from "./Message.ts";
|
||||||
import { iconHashToBigInt } from "../util/hash.ts";
|
import { iconHashToBigInt } from "../util/hash.ts";
|
||||||
import User from "./User.ts";
|
import User from "./User.ts";
|
||||||
|
import Message from "./Message.ts";
|
||||||
|
import * as Routes from "../util/Routes.ts";
|
||||||
|
|
||||||
export class Webhook implements Model {
|
export class Webhook implements Model {
|
||||||
constructor(session: Session, data: DiscordWebhook) {
|
constructor(session: Session, data: DiscordWebhook) {
|
||||||
@ -42,6 +46,62 @@ export class Webhook implements Model {
|
|||||||
channelId?: Snowflake;
|
channelId?: Snowflake;
|
||||||
guildId?: Snowflake;
|
guildId?: Snowflake;
|
||||||
user?: User;
|
user?: User;
|
||||||
|
|
||||||
|
async execute(options?: WebhookOptions & CreateMessage & { avatarUrl?: string, username?: string }) {
|
||||||
|
if (!this.token) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = {
|
||||||
|
content: options?.content,
|
||||||
|
embeds: options?.embeds,
|
||||||
|
tts: options?.tts,
|
||||||
|
allowed_mentions: options?.allowedMentions,
|
||||||
|
// @ts-ignore: TODO: component builder or something
|
||||||
|
components: options?.components,
|
||||||
|
file: options?.files,
|
||||||
|
};
|
||||||
|
|
||||||
|
const message = await this.session.rest.sendRequest<DiscordMessage>(this.session.rest, {
|
||||||
|
url: Routes.WEBHOOK(this.id, this.token!, {
|
||||||
|
wait: options?.wait,
|
||||||
|
threadId: options?.threadId,
|
||||||
|
}),
|
||||||
|
method: "POST",
|
||||||
|
payload: this.session.rest.createRequestBody(this.session.rest, {
|
||||||
|
method: "POST",
|
||||||
|
body: {
|
||||||
|
...data,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
return (options?.wait ?? true) ? new Message(this.session, message) : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
async fetch() {
|
||||||
|
const message = await this.session.rest.runMethod<DiscordWebhook>(
|
||||||
|
this.session.rest,
|
||||||
|
"GET",
|
||||||
|
Routes.WEBHOOK_TOKEN(this.id, this.token),
|
||||||
|
);
|
||||||
|
|
||||||
|
return new Webhook(this.session, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
async fetchMessage(messageId: Snowflake) {
|
||||||
|
if (!this.token) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const message = await this.session.rest.runMethod<DiscordMessage>(
|
||||||
|
this.session.rest,
|
||||||
|
"GET",
|
||||||
|
Routes.WEBHOOK_MESSAGE(this.id, this.token, messageId),
|
||||||
|
);
|
||||||
|
|
||||||
|
return new Message(this.session, message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Webhook;
|
export default Webhook;
|
||||||
|
@ -3,6 +3,7 @@ import type { Snowflake } from "../../util/Snowflake.ts";
|
|||||||
import type { Session } from "../../session/Session.ts";
|
import type { Session } from "../../session/Session.ts";
|
||||||
import type { ApplicationCommandTypes, DiscordMemberWithUser, DiscordInteraction, InteractionTypes } from "../../vendor/external.ts";
|
import type { ApplicationCommandTypes, DiscordMemberWithUser, DiscordInteraction, InteractionTypes } from "../../vendor/external.ts";
|
||||||
import type { CreateMessage } from "../Message.ts";
|
import type { CreateMessage } from "../Message.ts";
|
||||||
|
import type { MessageFlags } from "../../util/shared/flags.ts";
|
||||||
import { InteractionResponseTypes } from "../../vendor/external.ts";
|
import { InteractionResponseTypes } from "../../vendor/external.ts";
|
||||||
import BaseInteraction from "./BaseInteraction.ts";
|
import BaseInteraction from "./BaseInteraction.ts";
|
||||||
import CommandInteractionOptionResolver from "./CommandInteractionOptionResolver.ts";
|
import CommandInteractionOptionResolver from "./CommandInteractionOptionResolver.ts";
|
||||||
@ -11,6 +12,8 @@ import User from "../User.ts";
|
|||||||
import Member from "../Member.ts";
|
import Member from "../Member.ts";
|
||||||
import Message from "../Message.ts";
|
import Message from "../Message.ts";
|
||||||
import Role from "../Role.ts";
|
import Role from "../Role.ts";
|
||||||
|
import Webhook from "../Webhook.ts";
|
||||||
|
import * as Routes from "../../util/Routes.ts";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @link https://discord.com/developers/docs/interactions/slash-commands#interaction-response
|
* @link https://discord.com/developers/docs/interactions/slash-commands#interaction-response
|
||||||
@ -23,12 +26,11 @@ export interface InteractionResponse {
|
|||||||
/**
|
/**
|
||||||
* @link https://discord.com/developers/docs/interactions/slash-commands#interaction-response-interactionapplicationcommandcallbackdata
|
* @link https://discord.com/developers/docs/interactions/slash-commands#interaction-response-interactionapplicationcommandcallbackdata
|
||||||
* */
|
* */
|
||||||
export interface InteractionApplicationCommandCallbackData extends Omit<CreateMessage, "messageReference"> {
|
export interface InteractionApplicationCommandCallbackData extends Pick<CreateMessage, "allowedMentions" | "content" | "embeds" | "files"> {
|
||||||
customId?: string;
|
customId?: string;
|
||||||
title?: string;
|
title?: string;
|
||||||
// TODO: use builder
|
|
||||||
// components?: MessageComponents;
|
// components?: MessageComponents;
|
||||||
flags?: number;
|
flags?: MessageFlags;
|
||||||
choices?: ApplicationCommandOptionChoice[];
|
choices?: ApplicationCommandOptionChoice[];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,6 +105,46 @@ export class CommandInteraction extends BaseInteraction implements Model {
|
|||||||
};
|
};
|
||||||
options: CommandInteractionOptionResolver;
|
options: CommandInteractionOptionResolver;
|
||||||
responded = false;
|
responded = false;
|
||||||
|
|
||||||
|
async sendFollowUp(options: InteractionApplicationCommandCallbackData): Promise<Message> {
|
||||||
|
const message = await Webhook.prototype.execute.call({
|
||||||
|
id: this.applicationId!,
|
||||||
|
token: this.token,
|
||||||
|
session: this.session,
|
||||||
|
}, options);
|
||||||
|
|
||||||
|
return message!;
|
||||||
|
}
|
||||||
|
|
||||||
|
async respond({ type, data: options }: InteractionResponse): Promise<Message | undefined> {
|
||||||
|
const data = {
|
||||||
|
content: options?.content,
|
||||||
|
custom_id: options?.customId,
|
||||||
|
file: options?.files,
|
||||||
|
allowed_mentions: options?.allowedMentions,
|
||||||
|
flags: options?.flags,
|
||||||
|
chocies: options?.choices,
|
||||||
|
embeds: options?.embeds,
|
||||||
|
title: options?.title,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!this.respond) {
|
||||||
|
await this.session.rest.sendRequest<undefined>(this.session.rest, {
|
||||||
|
url: Routes.INTERACTION_ID_TOKEN(this.id, this.token),
|
||||||
|
method: "POST",
|
||||||
|
payload: this.session.rest.createRequestBody(this.session.rest, {
|
||||||
|
method: "POST",
|
||||||
|
body: { type, data, file: options?.files },
|
||||||
|
headers: { "Authorization": "" },
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
this.responded = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.sendFollowUp(data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default CommandInteraction;
|
export default CommandInteraction;
|
||||||
|
@ -144,11 +144,26 @@ export function INTERACTION_ID_TOKEN(interactionId: Snowflake, token: string) {
|
|||||||
return `/interactions/${interactionId}/${token}/callback`;
|
return `/interactions/${interactionId}/${token}/callback`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function WEBHOOK(webhookId: Snowflake, token: string, options?: { wait?: boolean; threadId?: Snowflake }) {
|
export function WEBHOOK_MESSAGE(webhookId: Snowflake, token: string, messageId: Snowflake) {
|
||||||
let url = `/webhooks/${webhookId}/${token}?`;
|
return `/webhooks/${webhookId}/${token}/messages/${messageId}`;
|
||||||
|
}
|
||||||
|
|
||||||
if (options?.wait !== undefined) url += `wait=${options.wait}`;
|
export function WEBHOOK_TOKEN(webhookId: Snowflake, token?: string) {
|
||||||
if (options?.threadId) url += `threadId=${options.threadId}`;
|
if (!token) return `/webhooks/${webhookId}`;
|
||||||
|
return `/webhooks/${webhookId}/${token}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface WebhookOptions {
|
||||||
|
wait?: boolean;
|
||||||
|
threadId?: Snowflake;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function WEBHOOK(webhookId: Snowflake, token: string, options?: WebhookOptions) {
|
||||||
|
let url = `/webhooks/${webhookId}/${token}`;
|
||||||
|
|
||||||
|
if (options?.wait) url += `?wait=${options.wait}`;
|
||||||
|
if (options?.threadId) url += `?threadId=${options.threadId}`;
|
||||||
|
if (options?.wait && options.threadId) url += `?wait=${options.wait}&threadId=${options.threadId}`;
|
||||||
|
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user