feat: invites

This commit is contained in:
Yuzu 2022-06-26 17:43:39 -05:00
parent 4b85b05c7f
commit 0e82520f15
6 changed files with 168 additions and 8 deletions

View File

@ -4,7 +4,8 @@ import type { DiscordGuild, GuildNsfwLevel, VerificationLevels } from "../vendor
import { iconHashToBigInt } from "../util/hash.ts"; import { iconHashToBigInt } from "../util/hash.ts";
import { BaseGuild } from "./BaseGuild.ts"; import { BaseGuild } from "./BaseGuild.ts";
export abstract 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: DiscordGuild) { constructor(session: Session, data: DiscordGuild) {
super(session, data); super(session, data);

67
structures/Invite.ts Normal file
View File

@ -0,0 +1,67 @@
import type { Session } from "../session/Session.ts";
import type { DiscordInvite } from "../vendor/external.ts";
import { TargetTypes } from "../vendor/external.ts";
import InviteGuild from "./InviteGuild.ts";
import User from "./User.ts";
/**
* @link https://discord.com/developers/docs/resources/invite#invite-object
*/
export class Invite {
constructor(session: Session, data: DiscordInvite) {
this.session = session;
if (data.guild) {
this.guild = new InviteGuild(session, data.guild);
}
if (data.approximate_member_count) {
this.approximateMemberCount = data.approximate_member_count;
}
if (data.approximate_presence_count) {
this.approximatePresenceCount = data.approximate_presence_count;
}
// TODO: fix this
// this.channel = data.channel;
this.code = data.code;
if (data.expires_at) {
this.expiresAt = Number.parseInt(data.expires_at);
}
// TODO: fix this
// this.xd = data.guild_scheduled_event
if (data.inviter) {
this.inviter = new User(session, data.inviter);
}
if (data.target_user) {
this.targetUser = new User(session, data.target_user);
}
// TODO: fix this
// this.stageInstance = data.stage_instance
// TODO: fix this
// this.targetApplication = data.target_application
if (data.target_type) {
this.targetType = data.target_type;
}
}
readonly session: Session;
guild?: InviteGuild;
approximateMemberCount?: number;
approximatePresenceCount?: number;
code: string;
expiresAt?: number;
inviter?: User;
targetUser?: User;
targeType?: TargetTypes;
}
export default Invite;

18
structures/InviteGuild.ts Normal file
View File

@ -0,0 +1,18 @@
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 {
constructor(session: Session, data: Partial<DiscordGuild>) {
super(session, data);
if (data.welcome_screen) {
this.welcomeScreen = new WelcomeScreen(session, data.welcome_screen);
}
}
welcomeScreen?: WelcomeScreen;
}
export default InviteGuild;

View File

@ -1,11 +1,12 @@
import type { Session } from "../session/Session.ts"; import type { Session } from "../session/Session.ts";
import type { Snowflake } from "../util/Snowflake.ts"; import type { Snowflake } from "../util/Snowflake.ts";
import type { GetMessagesOptions } from "../util/Routes.ts"; import type { GetMessagesOptions } from "../util/Routes.ts";
import type { DiscordChannel, DiscordInviteCreate, DiscordMessage } from "../vendor/external.ts"; import type { DiscordChannel, DiscordInvite, DiscordMessage, TargetTypes } from "../vendor/external.ts";
import { GuildChannel } from "./GuildChannel.ts"; import { GuildChannel } from "./GuildChannel.ts";
import { Guild } from "./Guild.ts"; import { Guild } from "./Guild.ts";
import { ThreadChannel } from "./ThreadChannel.ts"; import { ThreadChannel } from "./ThreadChannel.ts";
import { Message } from "./Message.ts"; import { Message } from "./Message.ts";
import { Invite } from "./Invite.ts";
import { Routes } from "../util/mod.ts"; import { Routes } from "../util/mod.ts";
/** /**
@ -13,11 +14,14 @@ import { Routes } from "../util/mod.ts";
* @link https://discord.com/developers/docs/resources/channel#create-channel-invite-json-params * @link https://discord.com/developers/docs/resources/channel#create-channel-invite-json-params
*/ */
export interface DiscordInviteOptions { export interface DiscordInviteOptions {
max_age?: number; maxAge?: number;
max_uses?: number; maxUses?: number;
unique?: boolean; unique?: boolean;
temporary: boolean; temporary: boolean;
reason?: string; reason?: string;
targetType?: TargetTypes;
targetUserId?: Snowflake;
targetApplicationId?: Snowflake;
} }
/** /**
@ -54,14 +58,26 @@ export class TextChannel extends GuildChannel {
); );
return messages[0] ? messages.map((x: DiscordMessage) => new Message(this.session, x)) : []; return messages[0] ? messages.map((x: DiscordMessage) => new Message(this.session, x)) : [];
} }
// TODO return Invite Class
createInvite(options?: DiscordInviteOptions) { async createInvite(options?: DiscordInviteOptions) {
return this.session.rest.runMethod<DiscordInviteCreate>( const invite = await this.session.rest.runMethod<DiscordInvite>(
this.session.rest, this.session.rest,
"POST", "POST",
Routes.CHANNEL_INVITES(this.id), Routes.CHANNEL_INVITES(this.id),
options, options
? {
max_age: options.maxAge,
max_uses: options.maxUses,
temporary: options.temporary,
unique: options.unique,
target_type: options.targetType,
target_user_id: options.targetUserId,
target_application_id: options.targetApplicationId,
}
: {},
); );
return new Invite(this.session, invite);
} }
async createThread(options: ThreadCreateOptions): Promise<ThreadChannel> { async createThread(options: ThreadCreateOptions): Promise<ThreadChannel> {

View File

@ -0,0 +1,32 @@
import type { Model } from "./Base.ts";
import type { Snowflake } from "../util/Snowflake.ts";
import type { Session } from "../session/Session.ts";
import type { DiscordWelcomeScreenChannel } from "../vendor/external.ts";
import Emoji from "./Emoji.ts";
/**
* @link https://discord.com/developers/docs/resources/guild#welcome-screen-object-welcome-screen-channel-structure
*/
export class WelcomeChannel implements Model {
constructor(session: Session, data: DiscordWelcomeScreenChannel) {
this.session = session;
this.channelId = data.channel_id;
this.description = data.description;
this.emoji = new Emoji(session, {
name: data.emoji_name ? data.emoji_name : undefined,
id: data.emoji_id ? data.emoji_id : undefined,
});
}
session: Session;
channelId: Snowflake;
description: string;
emoji: Emoji;
/** alias for WelcomeScreenChannel.channelId */
get id() {
return this.channelId;
}
}
export default WelcomeChannel;

View File

@ -0,0 +1,26 @@
import type { Session } from "../session/Session.ts";
import type { DiscordWelcomeScreen } from "../vendor/external.ts";
import WelcomeChannel from "./WelcomeChannel.ts";
/**
* @link https://discord.com/developers/docs/resources/guild#welcome-screen-object
*/
export class WelcomeScreen {
constructor(session: Session, data: DiscordWelcomeScreen) {
this.session = session;
this.welcomeChannels = data.welcome_channels.map((welcomeChannel) =>
new WelcomeChannel(session, welcomeChannel)
);
if (data.description) {
this.description = data.description;
}
}
readonly session: Session;
description?: string;
welcomeChannels: WelcomeChannel[];
}
export default WelcomeScreen;