Files
discordeno/helpers/webhooks/editWebhookMessage.ts
Dorian Oszczęda c9659bca4c feat!: Add return types to helper functions. (#2395)
* feat: Add return types to forum channel helpers.

* fix: Use Discord-defined property names. Add `flags` property.

* feat: Add return types to thread channel helpers.

* feat: Add return types to channel helpers.

* feat: Add return types to server discovery helpers.

* misc!: Consistency: 'remove' -> 'delete'.

* misc!: Add `get` keyword to function and file name.

* feat: Add return types to emoji helpers.

* misc!: Remove unused `bot` parameter. Capitalise 'URL'.

* feat: Add return types to guild automod helpers.

* feat: Add return types to guild scheduled event helpers.

* misc!: Consistency: Rename `emojiURL` to `getEmojiURL`.

* feat: Add return types to guild helpers.

* misc!: Consistency: Add 'get' keyword to function and file names.

* feat: Add return types to invite helpers.

* feat: Add return types to integration helpers.

* feat: Add return types to application command helpers.

* feat: Add return types to followup message helpers.

* feat: Add return types to interaction response helpers.

* feat: Add return type to `createInvite()`.

* feat: Add return types to member helpers.

* misc!: Consistency: Add 'get' keyword to function and file names.

* feat: Add return types to message helpers.

* misc!: Consistency: 'remove' -> 'delete'.

* fix: Use `slice()` to prevent unwanted side effects.

* feat: Add return types to miscellaneous helpers.

* misc!: Consistency: Add 'get' keyword to function and file names.

* feat: Add return types to role helpers.

* feat: Add return types to oauth helpers.

* feat: Add return types to template helpers.

* misc!: Consistency: Add 'guild' keyword to name.

* feat: Add return types to voice helpers.

* fix: Name function correctly.

* feat: Add return types to webhook helpers.

* misc!: Consistency: Rename `sendWebhook` to `sendWebhookMessage`.

* misc: Update exports.

* fix: Imports.

* fix: Change return collection key type from `string` to `bigint`. Remove redundant code.

* misc: Remove `undefined` from `runMethod()` return type.

* style: Remove redundant types.

* style: Rename endpoint call result variables to `result` and `results`.

* misc: Reintroduce `bot` as first parameter.

* misc: Adapt tests to changes made to helpers.

* fix: Object being transformed twice.

* style: Improve naming consistency of remaining files.

* misc: `bigint` -> `number`.

* style: Remove explicit `undefined` return.

* style: Remove `void` operator in favour of generic type.

* misc: Add missing `await` keyword.

Co-authored-by: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com>

* style: Make `reason` property optional instead of `undefined`-able.

* misc: Write out properties manually instead of using the spread operator.

Co-authored-by: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com>
2022-08-28 07:17:08 -04:00

122 lines
4.7 KiB
TypeScript

import type { Bot } from "../../bot.ts";
import { Attachment } from "../../transformers/attachment.ts";
import { Embed } from "../../transformers/embed.ts";
import { Message } from "../../transformers/message.ts";
import { DiscordMessage } from "../../types/discord.ts";
import { AllowedMentions, FileContent, MessageComponents } from "../../types/discordeno.ts";
import { MessageComponentTypes } from "../../types/shared.ts";
export async function editWebhookMessage(
bot: Bot,
webhookId: bigint,
webhookToken: string,
options: EditWebhookMessage & { messageId?: bigint; threadId?: bigint },
): Promise<Message> {
const result = await bot.rest.runMethod<DiscordMessage>(
bot.rest,
"PATCH",
options.messageId
? bot.constants.routes.WEBHOOK_MESSAGE(webhookId, webhookToken, options.messageId, options)
: bot.constants.routes.WEBHOOK_MESSAGE_ORIGINAL(webhookId, webhookToken, options),
{
content: options.content,
embeds: options.embeds?.map((embed) => bot.transformers.reverse.embed(bot, embed)),
file: options.file,
allowed_mentions: options.allowedMentions
? {
parse: options.allowedMentions.parse,
roles: options.allowedMentions.roles?.map((id) => id.toString()),
users: options.allowedMentions.users?.map((id) => id.toString()),
replied_user: options.allowedMentions.repliedUser,
}
: undefined,
attachments: options.attachments?.map((attachment) => ({
id: attachment.id.toString(),
filename: attachment.filename,
content_type: attachment.contentType,
size: attachment.size,
url: attachment.url,
proxy_url: attachment.proxyUrl,
height: attachment.height,
width: attachment.width,
ephemeral: attachment.ephemeral,
})),
components: options.components?.map((component) => ({
type: component.type,
components: component.components.map((subComponent) => {
if (subComponent.type === MessageComponentTypes.InputText) {
return {
type: subComponent.type,
style: subComponent.style,
custom_id: subComponent.customId,
label: subComponent.label,
placeholder: subComponent.placeholder,
min_length: subComponent.minLength ?? subComponent.required === false ? 0 : subComponent.minLength,
max_length: subComponent.maxLength,
};
}
if (subComponent.type === MessageComponentTypes.SelectMenu) {
return {
type: subComponent.type,
custom_id: subComponent.customId,
placeholder: subComponent.placeholder,
min_values: subComponent.minValues,
max_values: subComponent.maxValues,
disabled: "disabled" in subComponent ? subComponent.disabled : undefined,
options: subComponent.options.map((option) => ({
label: option.label,
value: option.value,
description: option.description,
emoji: option.emoji
? {
id: option.emoji.id?.toString(),
name: option.emoji.name,
animated: option.emoji.animated,
}
: undefined,
default: option.default,
})),
};
}
return {
type: subComponent.type,
custom_id: subComponent.customId,
label: subComponent.label,
style: subComponent.style,
emoji: "emoji" in subComponent && subComponent.emoji
? {
id: subComponent.emoji.id?.toString(),
name: subComponent.emoji.name,
animated: subComponent.emoji.animated,
}
: undefined,
url: "url" in subComponent ? subComponent.url : undefined,
disabled: "disabled" in subComponent ? subComponent.disabled : undefined,
};
}),
})),
message_id: options.messageId?.toString(),
},
);
return bot.transformers.message(bot, result);
}
/** https://discord.com/developers/docs/resources/webhook#edit-webhook-message-jsonform-params */
export interface EditWebhookMessage {
/** The message contents (up to 2000 characters) */
content?: string;
/** Embedded `rich` content */
embeds?: Embed[];
/** The contents of the file being sent/edited */
file?: FileContent | FileContent[];
/** Allowed mentions for the message */
allowedMentions?: AllowedMentions;
/** Attached files to keep */
attachments?: Attachment[];
/** The components you would like to have sent in this message */
components?: MessageComponents;
}