feat: new guild methods

This commit is contained in:
Yuzu 2022-06-27 22:47:05 -05:00
parent de34034ecc
commit 68f89bb589
12 changed files with 94 additions and 25 deletions

View File

@ -1,8 +1,11 @@
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 { DiscordGuild, GuildNsfwLevel, VerificationLevels } from "../vendor/external.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 BaseGuild from "./BaseGuild.ts";
import * as Routes from "../util/Routes.ts";
export class AnonymousGuild extends BaseGuild implements Model { export class AnonymousGuild extends BaseGuild implements Model {
constructor(session: Session, data: Partial<DiscordGuild>); // TODO: Improve this type (name and id are required) 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; description?: string;
premiumSubscriptionCount?: number; 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; export default AnonymousGuild;

View File

@ -12,6 +12,7 @@ export abstract class Channel implements Model {
} }
readonly id: Snowflake; readonly id: Snowflake;
readonly session: Session; readonly session: Session;
name?: string; name?: string;
type: ChannelTypes; type: ChannelTypes;

View File

@ -13,6 +13,7 @@ export class Emoji {
} }
readonly id?: Snowflake; readonly id?: Snowflake;
readonly session: Session; readonly session: Session;
name?: string; name?: string;
animated: boolean; animated: boolean;
available: boolean; available: boolean;

View File

@ -1,3 +1,4 @@
import type { Model } from "./Base.ts";
import type { Snowflake } from "../util/Snowflake.ts"; import type { Snowflake } from "../util/Snowflake.ts";
import type { Session } from "../session/Session.ts"; import type { Session } from "../session/Session.ts";
import type { DiscordEmoji, DiscordGuild, DiscordInviteMetadata, DiscordRole } from "../vendor/external.ts"; import type { DiscordEmoji, DiscordGuild, DiscordInviteMetadata, DiscordRole } from "../vendor/external.ts";
@ -41,7 +42,7 @@ export interface ModifyGuildEmoji {
* Represents a guild * Represents a guild
* @link https://discord.com/developers/docs/resources/guild#guild-object * @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) { constructor(session: Session, data: DiscordGuild) {
super(session, data); super(session, data);

View File

@ -1,10 +1,11 @@
import type { Model } from "./Base.ts";
import type { Snowflake } from "../util/Snowflake.ts"; import type { Snowflake } from "../util/Snowflake.ts";
import type { Session } from "../session/Session.ts"; import type { Session } from "../session/Session.ts";
import type { DiscordChannel } from "../vendor/external.ts"; import type { DiscordChannel } from "../vendor/external.ts";
import Channel from "./Channel.ts"; import Channel from "./Channel.ts";
import * as Routes from "../util/Routes.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) { constructor(session: Session, data: DiscordChannel, guildId: Snowflake) {
super(session, data); super(session, data);
this.guildId = guildId; this.guildId = guildId;

View File

@ -1,3 +1,4 @@
import type { Model } from "./Base.ts";
import type { Snowflake } from "../util/Snowflake.ts"; import type { Snowflake } from "../util/Snowflake.ts";
import type { Session } from "../session/Session.ts"; import type { Session } from "../session/Session.ts";
import type { DiscordEmoji } from "../vendor/external.ts"; import type { DiscordEmoji } from "../vendor/external.ts";
@ -7,7 +8,7 @@ import Emoji from "./Emoji.ts";
import User from "./User.ts"; import User from "./User.ts";
import * as Routes from "../util/Routes.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) { constructor(session: Session, data: DiscordEmoji, guildId: Snowflake) {
super(session, data); super(session, data);
this.guildId = guildId; this.guildId = guildId;
@ -20,6 +21,7 @@ export class GuildEmoji extends Emoji {
roles?: Snowflake[]; roles?: Snowflake[];
user?: User; user?: User;
managed?: boolean; managed?: boolean;
// id cannot be null in a GuildEmoji // id cannot be null in a GuildEmoji
override id: Snowflake; override id: Snowflake;

View File

@ -1,9 +1,10 @@
import type { Model } from "./Base.ts";
import type { Session } from "../session/Session.ts"; import type { Session } from "../session/Session.ts";
import type { DiscordGuild } from "../vendor/external.ts"; import type { DiscordGuild } from "../vendor/external.ts";
import AnonymousGuild from "./AnonymousGuild.ts"; import AnonymousGuild from "./AnonymousGuild.ts";
import WelcomeScreen from "./WelcomeScreen.ts"; import WelcomeScreen from "./WelcomeScreen.ts";
export class InviteGuild extends AnonymousGuild { export class InviteGuild extends AnonymousGuild implements Model {
constructor(session: Session, data: Partial<DiscordGuild>) { constructor(session: Session, data: Partial<DiscordGuild>) {
super(session, data); super(session, data);

View File

@ -1,7 +1,7 @@
import type { Model } from "./Base.ts"; import type { Model } from "./Base.ts";
import type { Snowflake } from "../util/Snowflake.ts"; import type { Snowflake } from "../util/Snowflake.ts";
import type { Session } from "../session/Session.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 { MessageFlags } from "../util/shared/flags.ts";
import User from "./User.ts"; import User from "./User.ts";
import Member from "./Member.ts"; import Member from "./Member.ts";
@ -18,21 +18,40 @@ export interface AllowedMentions {
users?: Snowflake[]; users?: Snowflake[];
} }
/** export interface CreateMessageReference {
* @link https://discord.com/developers/docs/resources/channel#edit-message-json-params messageId: Snowflake;
*/ channelId?: Snowflake;
export interface EditMessage { guildId?: Snowflake;
content?: string; failIfNotExists?: boolean;
allowedMentions?: AllowedMentions;
flags?: MessageFlags;
} }
/** /**
* @link https://discord.com/developers/docs/resources/channel#create-message-json-params * @link https://discord.com/developers/docs/resources/channel#create-message-json-params
*/ */
export interface CreateMessage { export interface CreateMessage {
content?: string; content: string;
allowedMentions?: AllowedMentions; 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 */ /** Replies directly in the channel the message was sent */
async reply({ content, allowedMentions }: CreateMessage): Promise<Message> { async reply(options: CreateMessage): Promise<Message> {
const message = await this.session.rest.runMethod( const message = await this.session.rest.runMethod<DiscordMessage>(
this.session.rest, this.session.rest,
"POST", "POST",
Routes.CHANNEL_MESSAGES(this.channelId), Routes.CHANNEL_MESSAGES(this.channelId),
{ {
content, content: options.content,
file: options.files,
allowed_mentions: { allowed_mentions: {
parse: allowedMentions?.parse, parse: options.allowedMentions?.parse,
roles: allowedMentions?.roles, roles: options.allowedMentions?.roles,
users: allowedMentions?.users, users: options.allowedMentions?.users,
replied_user: allowedMentions?.repliedUser, 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 { inGuild(): this is { guildId: Snowflake } & Message {

View File

@ -1,9 +1,10 @@
import type { Model } from "./Base.ts";
import type { Snowflake } from "../util/Snowflake.ts"; import type { Snowflake } from "../util/Snowflake.ts";
import type { Session } from "../session/Session.ts"; import type { Session } from "../session/Session.ts";
import type { DiscordChannel } from "../vendor/external.ts"; import type { DiscordChannel } from "../vendor/external.ts";
import GuildChannel from "./GuildChannel.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) { constructor(session: Session, data: DiscordChannel, guildId: Snowflake) {
super(session, data, guildId); super(session, data, guildId);
this.archived = !!data.thread_metadata?.archived; this.archived = !!data.thread_metadata?.archived;

View File

@ -4,6 +4,7 @@ import type { Session } from "../session/Session.ts";
import type { DiscordUser } from "../vendor/external.ts"; import type { DiscordUser } from "../vendor/external.ts";
import type { ImageFormat, ImageSize } from "../util/shared/images.ts"; import type { ImageFormat, ImageSize } from "../util/shared/images.ts";
import { iconBigintToHash, iconHashToBigInt } from "../util/hash.ts"; import { iconBigintToHash, iconHashToBigInt } from "../util/hash.ts";
import { formatImageUrl } from "../util/shared/images.ts";
import * as Routes from "../util/Routes.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)); 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() { toString() {

View File

@ -15,3 +15,11 @@ export function USER_DEFAULT_AVATAR(
) { ) {
return `${Endpoints.CDN_URL}/embed/avatars/${altIcon}.png`; 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}`;
}

View File

@ -7,3 +7,8 @@ export type ImageFormat = "jpg" | "jpeg" | "png" | "webp" | "gif" | "json";
* @link https://discord.com/developers/docs/reference#image-formatting * @link https://discord.com/developers/docs/reference#image-formatting
*/ */
export type ImageSize = 16 | 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096; 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}`;
}