mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-03 05:26:07 +00:00
feat: new guild methods
This commit is contained in:
parent
de34034ecc
commit
68f89bb589
@ -1,8 +1,11 @@
|
||||
import type { Model } from "./Base.ts";
|
||||
import type { Session } from "../session/Session.ts";
|
||||
import type { DiscordGuild, GuildNsfwLevel, VerificationLevels } from "../vendor/external.ts";
|
||||
import { iconHashToBigInt } from "../util/hash.ts";
|
||||
import type { ImageFormat, ImageSize } from "../util/shared/images.ts";
|
||||
import { iconBigintToHash, iconHashToBigInt } from "../util/hash.ts";
|
||||
import { formatImageUrl } from "../util/shared/images.ts";
|
||||
import BaseGuild from "./BaseGuild.ts";
|
||||
import * as Routes from "../util/Routes.ts";
|
||||
|
||||
export class AnonymousGuild extends BaseGuild implements Model {
|
||||
constructor(session: Session, data: Partial<DiscordGuild>); // TODO: Improve this type (name and id are required)
|
||||
@ -28,7 +31,25 @@ export class AnonymousGuild extends BaseGuild implements Model {
|
||||
description?: string;
|
||||
premiumSubscriptionCount?: number;
|
||||
|
||||
// TODO: bannerUrl and splashUrl
|
||||
splashUrl(options: { size?: ImageSize, format?: ImageFormat } = { size: 128 }) {
|
||||
if (this.splashHash) {
|
||||
return formatImageUrl(
|
||||
Routes.GUILD_SPLASH(this.id, iconBigintToHash(this.splashHash)),
|
||||
options.size,
|
||||
options.format
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
bannerUrl(options: { size?: ImageSize, format?: ImageFormat } = { size: 128 }) {
|
||||
if (this.bannerHash) {
|
||||
return formatImageUrl(
|
||||
Routes.GUILD_BANNER(this.id, iconBigintToHash(this.bannerHash)),
|
||||
options.size,
|
||||
options.format
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default AnonymousGuild;
|
||||
|
@ -12,6 +12,7 @@ export abstract class Channel implements Model {
|
||||
}
|
||||
readonly id: Snowflake;
|
||||
readonly session: Session;
|
||||
|
||||
name?: string;
|
||||
type: ChannelTypes;
|
||||
|
||||
|
@ -13,6 +13,7 @@ export class Emoji {
|
||||
}
|
||||
readonly id?: Snowflake;
|
||||
readonly session: Session;
|
||||
|
||||
name?: string;
|
||||
animated: boolean;
|
||||
available: boolean;
|
||||
|
@ -1,3 +1,4 @@
|
||||
import type { Model } from "./Base.ts";
|
||||
import type { Snowflake } from "../util/Snowflake.ts";
|
||||
import type { Session } from "../session/Session.ts";
|
||||
import type { DiscordEmoji, DiscordGuild, DiscordInviteMetadata, DiscordRole } from "../vendor/external.ts";
|
||||
@ -41,7 +42,7 @@ export interface ModifyGuildEmoji {
|
||||
* Represents a guild
|
||||
* @link https://discord.com/developers/docs/resources/guild#guild-object
|
||||
*/
|
||||
export class Guild extends BaseGuild {
|
||||
export class Guild extends BaseGuild implements Model {
|
||||
constructor(session: Session, data: DiscordGuild) {
|
||||
super(session, data);
|
||||
|
||||
|
@ -1,10 +1,11 @@
|
||||
import type { Model } from "./Base.ts";
|
||||
import type { Snowflake } from "../util/Snowflake.ts";
|
||||
import type { Session } from "../session/Session.ts";
|
||||
import type { DiscordChannel } from "../vendor/external.ts";
|
||||
import Channel from "./Channel.ts";
|
||||
import * as Routes from "../util/Routes.ts";
|
||||
|
||||
export abstract class GuildChannel extends Channel {
|
||||
export abstract class GuildChannel extends Channel implements Model {
|
||||
constructor(session: Session, data: DiscordChannel, guildId: Snowflake) {
|
||||
super(session, data);
|
||||
this.guildId = guildId;
|
||||
|
@ -1,3 +1,4 @@
|
||||
import type { Model } from "./Base.ts";
|
||||
import type { Snowflake } from "../util/Snowflake.ts";
|
||||
import type { Session } from "../session/Session.ts";
|
||||
import type { DiscordEmoji } from "../vendor/external.ts";
|
||||
@ -7,7 +8,7 @@ import Emoji from "./Emoji.ts";
|
||||
import User from "./User.ts";
|
||||
import * as Routes from "../util/Routes.ts";
|
||||
|
||||
export class GuildEmoji extends Emoji {
|
||||
export class GuildEmoji extends Emoji implements Model {
|
||||
constructor(session: Session, data: DiscordEmoji, guildId: Snowflake) {
|
||||
super(session, data);
|
||||
this.guildId = guildId;
|
||||
@ -20,6 +21,7 @@ export class GuildEmoji extends Emoji {
|
||||
roles?: Snowflake[];
|
||||
user?: User;
|
||||
managed?: boolean;
|
||||
|
||||
// id cannot be null in a GuildEmoji
|
||||
override id: Snowflake;
|
||||
|
||||
|
@ -1,9 +1,10 @@
|
||||
import type { Model } from "./Base.ts";
|
||||
import type { Session } from "../session/Session.ts";
|
||||
import type { DiscordGuild } from "../vendor/external.ts";
|
||||
import AnonymousGuild from "./AnonymousGuild.ts";
|
||||
import WelcomeScreen from "./WelcomeScreen.ts";
|
||||
|
||||
export class InviteGuild extends AnonymousGuild {
|
||||
export class InviteGuild extends AnonymousGuild implements Model {
|
||||
constructor(session: Session, data: Partial<DiscordGuild>) {
|
||||
super(session, data);
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
import type { Model } from "./Base.ts";
|
||||
import type { Snowflake } from "../util/Snowflake.ts";
|
||||
import type { Session } from "../session/Session.ts";
|
||||
import type { AllowedMentionsTypes, DiscordMessage } from "../vendor/external.ts";
|
||||
import type { AllowedMentionsTypes, DiscordMessage, FileContent } from "../vendor/external.ts";
|
||||
import { MessageFlags } from "../util/shared/flags.ts";
|
||||
import User from "./User.ts";
|
||||
import Member from "./Member.ts";
|
||||
@ -18,21 +18,40 @@ export interface AllowedMentions {
|
||||
users?: Snowflake[];
|
||||
}
|
||||
|
||||
/**
|
||||
* @link https://discord.com/developers/docs/resources/channel#edit-message-json-params
|
||||
*/
|
||||
export interface EditMessage {
|
||||
content?: string;
|
||||
allowedMentions?: AllowedMentions;
|
||||
flags?: MessageFlags;
|
||||
export interface CreateMessageReference {
|
||||
messageId: Snowflake;
|
||||
channelId?: Snowflake;
|
||||
guildId?: Snowflake;
|
||||
failIfNotExists?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* @link https://discord.com/developers/docs/resources/channel#create-message-json-params
|
||||
*/
|
||||
export interface CreateMessage {
|
||||
content?: string;
|
||||
content: string;
|
||||
allowedMentions?: AllowedMentions;
|
||||
messageReference?: CreateMessageReference;
|
||||
}
|
||||
|
||||
export interface CreateMessage {
|
||||
allowedMentions?: AllowedMentions;
|
||||
files: FileContent[];
|
||||
messageReference?: CreateMessageReference;
|
||||
}
|
||||
|
||||
export interface CreateMessage {
|
||||
content: string;
|
||||
allowedMentions?: AllowedMentions;
|
||||
files: FileContent[];
|
||||
messageReference?: CreateMessageReference;
|
||||
}
|
||||
|
||||
/**
|
||||
* @link https://discord.com/developers/docs/resources/channel#edit-message-json-params
|
||||
*/
|
||||
export interface EditMessage extends Partial<CreateMessage> {
|
||||
flags?: MessageFlags;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -127,23 +146,30 @@ export class Message implements Model {
|
||||
}
|
||||
|
||||
/** Replies directly in the channel the message was sent */
|
||||
async reply({ content, allowedMentions }: CreateMessage): Promise<Message> {
|
||||
const message = await this.session.rest.runMethod(
|
||||
async reply(options: CreateMessage): Promise<Message> {
|
||||
const message = await this.session.rest.runMethod<DiscordMessage>(
|
||||
this.session.rest,
|
||||
"POST",
|
||||
Routes.CHANNEL_MESSAGES(this.channelId),
|
||||
{
|
||||
content,
|
||||
content: options.content,
|
||||
file: options.files,
|
||||
allowed_mentions: {
|
||||
parse: allowedMentions?.parse,
|
||||
roles: allowedMentions?.roles,
|
||||
users: allowedMentions?.users,
|
||||
replied_user: allowedMentions?.repliedUser,
|
||||
parse: options.allowedMentions?.parse,
|
||||
roles: options.allowedMentions?.roles,
|
||||
users: options.allowedMentions?.users,
|
||||
replied_user: options.allowedMentions?.repliedUser,
|
||||
},
|
||||
message_reference: options.messageReference ? {
|
||||
message_id: options.messageReference.messageId,
|
||||
channel_id: options.messageReference.channelId,
|
||||
guild_id: options.messageReference.guildId,
|
||||
fail_if_not_exists: options.messageReference.failIfNotExists ?? true,
|
||||
} : undefined,
|
||||
},
|
||||
);
|
||||
|
||||
return message;
|
||||
return new Message(this.session, message);
|
||||
}
|
||||
|
||||
inGuild(): this is { guildId: Snowflake } & Message {
|
||||
|
@ -1,9 +1,10 @@
|
||||
import type { Model } from "./Base.ts";
|
||||
import type { Snowflake } from "../util/Snowflake.ts";
|
||||
import type { Session } from "../session/Session.ts";
|
||||
import type { DiscordChannel } from "../vendor/external.ts";
|
||||
import GuildChannel from "./GuildChannel.ts";
|
||||
|
||||
export class ThreadChannel extends GuildChannel {
|
||||
export class ThreadChannel extends GuildChannel implements Model {
|
||||
constructor(session: Session, data: DiscordChannel, guildId: Snowflake) {
|
||||
super(session, data, guildId);
|
||||
this.archived = !!data.thread_metadata?.archived;
|
||||
|
@ -4,6 +4,7 @@ import type { Session } from "../session/Session.ts";
|
||||
import type { DiscordUser } from "../vendor/external.ts";
|
||||
import type { ImageFormat, ImageSize } from "../util/shared/images.ts";
|
||||
import { iconBigintToHash, iconHashToBigInt } from "../util/hash.ts";
|
||||
import { formatImageUrl } from "../util/shared/images.ts";
|
||||
import * as Routes from "../util/Routes.ts";
|
||||
|
||||
/**
|
||||
@ -50,7 +51,7 @@ export class User implements Model {
|
||||
url = Routes.USER_AVATAR(this.id, iconBigintToHash(this.avatarHash));
|
||||
}
|
||||
|
||||
return `${url}.${options.format ?? (url.includes("/a_") ? "gif" : "jpg")}?size=${options.size}`;
|
||||
return formatImageUrl(url, options.size, options.format);
|
||||
}
|
||||
|
||||
toString() {
|
||||
|
@ -15,3 +15,11 @@ export function USER_DEFAULT_AVATAR(
|
||||
) {
|
||||
return `${Endpoints.CDN_URL}/embed/avatars/${altIcon}.png`;
|
||||
}
|
||||
|
||||
export function GUILD_BANNER(guildId: Snowflake, icon: string) {
|
||||
return `${Endpoints.CDN_URL}/banners/${guildId}/${icon}`;
|
||||
}
|
||||
|
||||
export function GUILD_SPLASH(guildId: Snowflake, icon: string) {
|
||||
return `${Endpoints.CDN_URL}/splashes/${guildId}/${icon}`;
|
||||
}
|
||||
|
@ -7,3 +7,8 @@ export type ImageFormat = "jpg" | "jpeg" | "png" | "webp" | "gif" | "json";
|
||||
* @link https://discord.com/developers/docs/reference#image-formatting
|
||||
*/
|
||||
export type ImageSize = 16 | 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096;
|
||||
|
||||
/** Help format an image url */
|
||||
export function formatImageUrl(url: string, size: ImageSize = 128, format?: ImageFormat) {
|
||||
return `${url}.${format || (url.includes("/a_") ? "gif" : "jpg")}?size=${size}`;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user