Files
discordeno/src/transformers/message.ts
2021-10-09 18:06:47 +02:00

108 lines
4.2 KiB
TypeScript

import { Bot } from "../bot.ts";
import { Message } from "../types/messages/message.ts";
import { CHANNEL_MENTION_REGEX } from "../util/constants.ts";
import { SnakeCasedPropertiesDeep } from "../types/util.ts";
export function transformMessage(bot: Bot, data: SnakeCasedPropertiesDeep<Message>) {
return {
// UNTRANSFORMED STUFF HERE
content: data.content || "",
isBot: data.author.bot || false,
tag: `${data.author.username}#${data.author.discriminator.toString().padStart(4, "0")}`,
timestamp: Date.parse(data.timestamp),
editedTimestamp: data.edited_timestamp ? Date.parse(data.edited_timestamp) : undefined,
bitfield: (data.tts ? 1n : 0n) | (data.mention_everyone ? 2n : 0n) | (data.pinned ? 4n : 0n),
attachments: data.attachments,
embeds: data.embeds,
reactions: data.reactions,
type: data.type,
activity: data.activity,
application: data.application,
flags: data.flags,
interaction: data.interaction,
thread: data.thread,
components: data.components,
stickerItems: data.sticker_items,
// TRANSFORMED STUFF BELOW
id: bot.transformers.snowflake(data.id),
guildId: data.guild_id ? bot.transformers.snowflake(data.guild_id) : undefined,
channelId: bot.transformers.snowflake(data.channel_id),
webhookId: data.webhook_id ? bot.transformers.snowflake(data.webhook_id) : undefined,
authorId: bot.transformers.snowflake(data.author.id),
applicationId: data.application_id ? bot.transformers.snowflake(data.application_id) : undefined,
messageReference: data.message_reference
? {
messageId: data.message_reference.message_id
? bot.transformers.snowflake(data.message_reference.message_id)
: undefined,
channelId: data.message_reference.channel_id
? bot.transformers.snowflake(data.message_reference.channel_id)
: undefined,
guildId: data.message_reference.guild_id
? bot.transformers.snowflake(data.message_reference.guild_id)
: undefined,
}
: undefined,
mentionedUserIds: data.mentions ? data.mentions.map((m) => bot.transformers.snowflake(m.id)) : [],
mentionedRoleIds: data.mention_roles ? data.mention_roles.map((id) => bot.transformers.snowflake(id)) : [],
mentionedChannelIds: [
// Keep any ids tht discord sends
...(data.mention_channels ?? []).map((m) => bot.transformers.snowflake(m.id)),
// Add any other ids that can be validated in a channel mention format
...(data.content?.match(CHANNEL_MENTION_REGEX) || []).map((text) =>
// converts the <#123> into 123
bot.transformers.snowflake(text.substring(2, text.length - 1))
),
],
};
}
export interface DiscordenoMessage
extends Omit<
Message,
| "id"
| "webhookId"
| "timestamp"
| "editedTimestamp"
| "guildId"
| "channelId"
| "member"
| "author"
| "applicationId"
| "thread"
> {
id: bigint;
/** Whether or not this message was sent by a bot */
isBot: boolean;
/** The username#discrimnator for the user who sent this message */
tag: string;
/** Holds all the boolean toggles. */
bitfield: bigint;
// For better user experience
/** Id of the guild which the massage has been send in. "0n" if it a DM */
guildId: bigint;
/** id of the channel the message was sent in */
channelId: bigint;
/** If the message is generated by a webhook, this is the webhook's id */
webhookId?: bigint;
/** The id of the user who sent this message */
authorId: bigint;
/** If the message is a response to an Interaction, this is the id of the interaction's application */
applicationId?: bigint;
/** The message content for this message. Empty string if no content was sent like an attachment only. */
content: string;
/** Ids of users specifically mentioned in the message */
mentionedUserIds: bigint[];
/** Ids of roles specifically mentioned in this message */
mentionedRoleIds: bigint[];
/** Channels specifically mentioned in this message */
mentionedChannelIds?: bigint[];
/** When this message was sent */
timestamp: number;
/** When this message was edited (or undefined if never) */
editedTimestamp?: number;
}