Merge branch 'yuzudev:main' into main

This commit is contained in:
Marcos Susaña 2022-06-24 21:55:47 -04:00 committed by GitHub
commit 3246fd2b81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 197 additions and 13 deletions

View File

@ -0,0 +1,31 @@
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 { BaseGuild } from "./BaseGuild.ts";
export abstract class AnonymousGuild extends BaseGuild implements Model {
constructor(session: Session, data: DiscordGuild) {
super(session, data);
this.splashHash = data.splash ? iconHashToBigInt(data.splash) : undefined;
this.bannerHash = data.banner ? iconHashToBigInt(data.banner) : undefined;
this.verificationLevel = data.verification_level;
this.vanityUrlCode = data.vanity_url_code ? data.vanity_url_code : undefined;
this.nsfwLevel = data.nsfw_level;
this.description = data.description ? data.description : undefined;
this.premiumSubscriptionCount = data.premium_subscription_count;
}
splashHash?: bigint;
bannerHash?: bigint;
verificationLevel: VerificationLevels;
vanityUrlCode?: string;
nsfwLevel: GuildNsfwLevel;
description?: string;
premiumSubscriptionCount?: number;
// TODO: bannerUrl and splashUrl
}

40
structures/BaseGuild.ts Normal file
View File

@ -0,0 +1,40 @@
import type { Model } from "./Base.ts";
import type { Session } from "../session/Session.ts";
import type { DiscordGuild, GuildFeatures } from "../vendor/external.ts";
import { Snowflake } from "../util/Snowflake.ts";
import { iconHashToBigInt } from "../util/hash.ts";
/**
* Class for {@link Guild} and {@link AnonymousGuild}
* */
export abstract class BaseGuild implements Model {
constructor(session: Session, data: DiscordGuild) {
this.session = session;
this.id = data.id;
this.name = data.name;
this.iconHash = data.icon ? iconHashToBigInt(data.icon) : undefined;
this.features = data.features;
}
readonly session: Session;
readonly id: Snowflake;
name: string;
iconHash?: bigint;
features: GuildFeatures[];
get createdTimestamp() {
return Snowflake.snowflakeToTimestamp(this.id);
}
get createdAt() {
return new Date(this.createdTimestamp);
}
toString() {
return this.name;
}
}

View File

@ -1,18 +1,31 @@
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 { DiscordGuild, DiscordMember, MakeRequired } from "../vendor/external.ts"; import type { DiscordGuild, DiscordRole } from "../vendor/external.ts";
import { DefaultMessageNotificationLevels, ExplicitContentFilterLevels, VerificationLevels } from "../vendor/external.ts"; import { DefaultMessageNotificationLevels, ExplicitContentFilterLevels, VerificationLevels } from "../vendor/external.ts";
import { iconHashToBigInt, iconBigintToHash as _iconBigintToHash } from "../util/hash.ts"; import { iconHashToBigInt, iconBigintToHash } from "../util/hash.ts";
import { Member } from "./Member.ts"; import { Member } from "./Member.ts";
import { BaseGuild } from "./BaseGuild.ts";
import { Role } from "./Role.ts";
import { Routes } from "../util/mod.ts";
export class Guild implements Model { export interface CreateRole {
name?: string;
color?: number;
iconHash?: string | bigint;
unicodeEmoji?: string;
hoist?: boolean;
mentionable?: boolean;
}
/**
* Represents a guild
* @link https://discord.com/developers/docs/resources/guild#guild-object
* */
export class Guild extends BaseGuild implements Model {
constructor(session: Session, data: DiscordGuild) { constructor(session: Session, data: DiscordGuild) {
this.session = session; super(session, data);
this.id = data.id;
this.name = data.name;
this.iconHash = data.icon ? iconHashToBigInt(data.icon) : undefined;
this.splashHash = data.splash ? iconHashToBigInt(data.splash) : undefined; this.splashHash = data.splash ? iconHashToBigInt(data.splash) : undefined;
this.discoverySplashHash = data.discovery_splash ? iconHashToBigInt(data.discovery_splash) : undefined; this.discoverySplashHash = data.discovery_splash ? iconHashToBigInt(data.discovery_splash) : undefined;
this.ownerId = data.owner_id; this.ownerId = data.owner_id;
@ -21,14 +34,10 @@ export class Guild implements Model {
this.vefificationLevel = data.verification_level; this.vefificationLevel = data.verification_level;
this.defaultMessageNotificationLevel = data.default_message_notifications; this.defaultMessageNotificationLevel = data.default_message_notifications;
this.explicitContentFilterLevel = data.explicit_content_filter; this.explicitContentFilterLevel = data.explicit_content_filter;
this.members = data.members?.map((member) => new Member(session, member as MakeRequired<DiscordMember, "user">)) ?? []; this.members = data.members?.map((member) => new Member(session, { ...member, user: member.user! })) ?? [];
this.roles = data.roles.map((role) => new Role(session, this, role));
} }
readonly session: Session;
readonly id: Snowflake;
name: string;
iconHash?: bigint;
splashHash?: bigint; splashHash?: bigint;
discoverySplashHash?: bigint; discoverySplashHash?: bigint;
ownerId: Snowflake; ownerId: Snowflake;
@ -38,4 +47,38 @@ export class Guild implements Model {
defaultMessageNotificationLevel: DefaultMessageNotificationLevels; defaultMessageNotificationLevel: DefaultMessageNotificationLevels;
explicitContentFilterLevel: ExplicitContentFilterLevels; explicitContentFilterLevel: ExplicitContentFilterLevels;
members: Member[]; members: Member[];
roles: Role[];
async createRole(options: CreateRole) {
let icon: string | undefined;
if (options.iconHash) {
if (typeof options.iconHash === "string") {
icon = options.iconHash;
}
else {
icon = iconBigintToHash(options.iconHash);
}
}
const role = await this.session.rest.runMethod<DiscordRole>(
this.session.rest,
"PUT",
Routes.GUILD_ROLES(this.id),
{
name: options.name,
color: options.color,
icon,
unicode_emoji: options.unicodeEmoji,
hoist: options.hoist,
mentionable: options.mentionable,
}
);
return new Role(this.session, this, role);
}
async deleteRole(roleId: Snowflake): Promise<void> {
await this.session.rest.runMethod<undefined>(this.session.rest, "DELETE", Routes.GUILD_ROLE(this.id, roleId));
}
} }

62
structures/Role.ts Normal file
View File

@ -0,0 +1,62 @@
import type { Model } from "./Base.ts";
import type { Session } from "../session/Session.ts";
import type { DiscordRole } from "../vendor/external.ts";
import { Snowflake } from "../util/Snowflake.ts";
import { iconHashToBigInt } from "../util/hash.ts";
import { Guild } from "./Guild.ts";
export class Role implements Model {
constructor(session: Session, guild: Guild, data: DiscordRole) {
this.session = session;
this.id = data.id;
this.guild = guild;
this.hoist = data.hoist;
this.iconHash = data.icon ? iconHashToBigInt(data.icon) : undefined;
this.color = data.color;
this.name = data.name;
this.unicodeEmoji = data.unicode_emoji;
this.mentionable = data.mentionable;
this.managed = data.managed;
}
session: Session;
id: Snowflake;
guild: Guild;
hoist: boolean;
iconHash?: bigint;
color: number;
name: string;
unicodeEmoji?: string;
mentionable: boolean;
managed: boolean;
get createdTimestamp() {
return Snowflake.snowflakeToTimestamp(this.id);
}
get createdAt() {
return new Date(this.createdTimestamp);
}
get hexColor() {
return `#${this.color.toString(16).padStart(6, '0')}`;
}
/*
* delete() {
* return.this.guild.deleteRole(this.id);
* }
* edit() {
* return this.guild.editRole(this.id);
* }
* */
toString() {
switch (this.id) {
case this.guild.id:
return "@everyone";
default:
return `<@&${this.id}>`;
}
}
}

View File

@ -77,3 +77,11 @@ export function GUILD_BANS(guildId: Snowflake, options?: GetBans) {
return url; return url;
} }
export function GUILD_ROLE(guildId: Snowflake, roleId: Snowflake) {
return `/guilds/${guildId}/roles/${roleId}`;
}
export function GUILD_ROLES(guildId: Snowflake) {
return `/guilds/${guildId}/roles`;
}