This commit is contained in:
socram03 2022-07-08 23:10:49 -04:00
parent c1c358a411
commit 22b27c0874
4 changed files with 165 additions and 135 deletions

View File

@ -16,6 +16,8 @@ import type {
DiscordIntegration, DiscordIntegration,
DiscordIntegrationDelete, DiscordIntegrationDelete,
DiscordInteraction, DiscordInteraction,
DiscordInviteCreate,
DiscordInviteDelete,
DiscordMemberWithUser, DiscordMemberWithUser,
DiscordMessage, DiscordMessage,
DiscordMessageDelete, DiscordMessageDelete,
@ -25,15 +27,15 @@ import type {
DiscordMessageReactionRemoveEmoji, DiscordMessageReactionRemoveEmoji,
DiscordReady, DiscordReady,
DiscordRole, DiscordRole,
DiscordScheduledEvent,
DiscordScheduledEventUserAdd,
DiscordScheduledEventUserRemove,
// DiscordThreadMemberUpdate, // DiscordThreadMemberUpdate,
// DiscordThreadMembersUpdate, // DiscordThreadMembersUpdate,
DiscordThreadListSync, DiscordThreadListSync,
DiscordTypingStart, DiscordTypingStart,
DiscordUser, DiscordUser,
DiscordWebhookUpdate, DiscordWebhookUpdate,
DiscordInviteCreate,
DiscordInviteDelete,
DiscordScheduledEvent
} from "../discordeno/mod.ts"; } from "../discordeno/mod.ts";
import type { Snowflake } from "./Snowflake.ts"; import type { Snowflake } from "./Snowflake.ts";
@ -43,7 +45,7 @@ import type { Interaction } from "./structures/interactions/InteractionFactory.t
import { AutoModerationRule } from "./structures/AutoModerationRule.ts"; import { AutoModerationRule } from "./structures/AutoModerationRule.ts";
import { AutoModerationExecution } from "./structures/AutoModerationExecution.ts"; import { AutoModerationExecution } from "./structures/AutoModerationExecution.ts";
import { type Channel, ChannelFactory, GuildChannel, ThreadChannel } from "./structures/channels.ts"; import { type Channel, ChannelFactory, GuildChannel, ThreadChannel } from "./structures/channels.ts";
import { StageInstance, type DiscordStageInstance } from "./structures/StageInstance.ts"; import { type DiscordStageInstance, StageInstance } from "./structures/StageInstance.ts";
import { ScheduledEvent } from "./structures/GuildScheduledEvent.ts"; import { ScheduledEvent } from "./structures/GuildScheduledEvent.ts";
import ThreadMember from "./structures/ThreadMember.ts"; import ThreadMember from "./structures/ThreadMember.ts";
@ -53,7 +55,7 @@ import User from "./structures/User.ts";
import Integration from "./structures/Integration.ts"; import Integration from "./structures/Integration.ts";
import Guild from "./structures/guilds/Guild.ts"; import Guild from "./structures/guilds/Guild.ts";
import InteractionFactory from "./structures/interactions/InteractionFactory.ts"; import InteractionFactory from "./structures/interactions/InteractionFactory.ts";
import { NewInviteCreate, InviteCreate } from "./structures/Invite.ts"; import { InviteCreate, NewInviteCreate } from "./structures/Invite.ts";
export type RawHandler<T> = (...args: [Session, number, T]) => void; export type RawHandler<T> = (...args: [Session, number, T]) => void;
export type Handler<T extends unknown[]> = (...args: T) => unknown; export type Handler<T extends unknown[]> = (...args: T) => unknown;
@ -282,35 +284,59 @@ export const MESSAGE_REACTION_REMOVE_EMOJI: RawHandler<DiscordMessageReactionRem
export const INVITE_CREATE: RawHandler<DiscordInviteCreate> = (session, _shardId, invite) => { export const INVITE_CREATE: RawHandler<DiscordInviteCreate> = (session, _shardId, invite) => {
session.emit("inviteCreate", NewInviteCreate(session, invite)); session.emit("inviteCreate", NewInviteCreate(session, invite));
} };
export const INVITE_DELETE: RawHandler<DiscordInviteDelete> = (session, _shardId, data) => { export const INVITE_DELETE: RawHandler<DiscordInviteDelete> = (session, _shardId, data) => {
session.emit("inviteDelete", { channelId: data.channel_id, guildId: data.guild_id, code: data.code }); session.emit("inviteDelete", { channelId: data.channel_id, guildId: data.guild_id, code: data.code });
} };
export const STAGE_INSTANCE_CREATE: RawHandler<DiscordStageInstance> = (session, _shardId, payload) => { export const STAGE_INSTANCE_CREATE: RawHandler<DiscordStageInstance> = (session, _shardId, payload) => {
session.emit("stageInstanceCreate", new StageInstance(session, payload)); session.emit("stageInstanceCreate", new StageInstance(session, payload));
}; };
export const STAGE_INSTANCE_UPDATE: RawHandler<DiscordStageInstance> = (session, _shardId, payload) => { export const STAGE_INSTANCE_UPDATE: RawHandler<DiscordStageInstance> = (session, _shardId, payload) => {
session.emit("stageInstanceUpdate", new StageInstance(session, payload)); session.emit("stageInstanceUpdate", new StageInstance(session, payload));
}; };
export const STAGE_INSTANCE_DELETE: RawHandler<DiscordStageInstance> = (session, _shardId, payload) => { export const STAGE_INSTANCE_DELETE: RawHandler<DiscordStageInstance> = (session, _shardId, payload) => {
session.emit("stageInstanceDelete", new StageInstance(session, payload)); session.emit("stageInstanceDelete", new StageInstance(session, payload));
}; };
export const GUILD_SCHEDULED_EVENT_CREATE: RawHandler<DiscordScheduledEvent> = (session, _shardId, payload) => { export const GUILD_SCHEDULED_EVENT_CREATE: RawHandler<DiscordScheduledEvent> = (session, _shardId, payload) => {
session.emit("guildScheduledEventCreate", new ScheduledEvent(session, payload)) session.emit("guildScheduledEventCreate", new ScheduledEvent(session, payload));
} };
export const GUILD_SCHEDULED_EVENT_UPDATE: RawHandler<DiscordScheduledEvent> = (session, _shardId, payload) => { export const GUILD_SCHEDULED_EVENT_UPDATE: RawHandler<DiscordScheduledEvent> = (session, _shardId, payload) => {
session.emit("guildScheduledEventUpdate", new ScheduledEvent(session, payload)) session.emit("guildScheduledEventUpdate", new ScheduledEvent(session, payload));
} };
export const GUILD_SCHEDULED_EVENT_DELETE: RawHandler<DiscordScheduledEvent> = (session, _shardId, payload) => { export const GUILD_SCHEDULED_EVENT_DELETE: RawHandler<DiscordScheduledEvent> = (session, _shardId, payload) => {
session.emit("guildScheduledEventDelete", new ScheduledEvent(session, payload)) session.emit("guildScheduledEventDelete", new ScheduledEvent(session, payload));
} };
export const GUILD_SCHEDULED_EVENT_USER_ADD: RawHandler<DiscordScheduledEventUserAdd> = (
session,
_shardId,
payload,
) => {
session.emit("guildScheduledEventUserAdd", {
scheduledEventId: payload.guild_scheduled_event_id,
userId: payload.user_id,
guildId: payload.guild_id,
});
};
export const GUILD_SCHEDULED_EVENT_USER_REMOVE: RawHandler<DiscordScheduledEventUserRemove> = (
session,
_shardId,
payload,
) => {
session.emit("guildScheduledEventUserRemove", {
scheduledEventId: payload.guild_scheduled_event_id,
userId: payload.user_id,
guildId: payload.guild_id,
});
};
export const raw: RawHandler<unknown> = (session, shardId, data) => { export const raw: RawHandler<unknown> = (session, shardId, data) => {
session.emit("raw", data, shardId); session.emit("raw", data, shardId);
@ -369,6 +395,8 @@ export interface Events {
"guildScheduledEventCreate": Handler<[ScheduledEvent]>; "guildScheduledEventCreate": Handler<[ScheduledEvent]>;
"guildScheduledEventUpdate": Handler<[ScheduledEvent]>; "guildScheduledEventUpdate": Handler<[ScheduledEvent]>;
"guildScheduledEventDelete": Handler<[ScheduledEvent]>; "guildScheduledEventDelete": Handler<[ScheduledEvent]>;
"guildScheduledEventUserAdd": Handler<[{scheduledEventId: Snowflake, userId: Snowflake, guildId: Snowflake}]>
"guildScheduledEventUserRemove": Handler<[{scheduledEventId: Snowflake, userId: Snowflake, guildId: Snowflake}]>
"raw": Handler<[unknown, number]>; "raw": Handler<[unknown, number]>;
"webhooksUpdate": Handler<[{ guildId: Snowflake, channelId: Snowflake }]>; "webhooksUpdate": Handler<[{ guildId: Snowflake, channelId: Snowflake }]>;
"userUpdate": Handler<[User]>; "userUpdate": Handler<[User]>;

View File

@ -1,107 +1,106 @@
import { Model } from "./Base.ts"; import { Model } from "./Base.ts";
import type { Snowflake } from "../Snowflake.ts"; import type { Snowflake } from "../Snowflake.ts";
import type { Session } from "../Session.ts"; import type { Session } from "../Session.ts";
import { import {
DiscordApplication, DiscordApplication,
TeamMembershipStates, DiscordInstallParams,
DiscordInstallParams, DiscordTeam,
DiscordUser, DiscordUser,
DiscordTeam TeamMembershipStates,
} from "../../discordeno/mod.ts"; } from "../../discordeno/mod.ts";
import User from "./User.ts"; import User from "./User.ts";
type SummaryDeprecated = "" type SummaryDeprecated = "";
export interface Team { export interface Team {
/** a hash of the image of the team's icon */ /** a hash of the image of the team's icon */
icon?: string; icon?: string;
/** the unique id of the team */ /** the unique id of the team */
id: string; id: string;
/** the members of the team */ /** the members of the team */
members: TeamMember[]; members: TeamMember[];
/** user id of the current team owner */ /** user id of the current team owner */
ownerUserId: string; ownerUserId: string;
/** team name */ /** team name */
name: string; name: string;
} }
export interface TeamMember { export interface TeamMember {
/** the user's membership state on the team */ /** the user's membership state on the team */
membershipState: TeamMembershipStates; membershipState: TeamMembershipStates;
permissions: "*"[]; permissions: "*"[];
teamId: string; teamId: string;
user: Partial<User> & Pick<User, "avatarHash" | "discriminator" | "id" | "username"> user: Partial<User> & Pick<User, "avatarHash" | "discriminator" | "id" | "username">;
} }
// NewTeam create a new Team object for discord applications // NewTeam create a new Team object for discord applications
export function NewTeam(session: Session, data: DiscordTeam): Team { export function NewTeam(session: Session, data: DiscordTeam): Team {
return { return {
icon: data.icon ? data.icon : undefined, icon: data.icon ? data.icon : undefined,
id: data.id, id: data.id,
members: data.members.map(member => { members: data.members.map((member) => {
return { return {
membershipState: member.membership_state, membershipState: member.membership_state,
permissions: member.permissions, permissions: member.permissions,
teamId: member.team_id, teamId: member.team_id,
user: new User(session, member.user) user: new User(session, member.user),
} };
}), }),
ownerUserId: data.owner_user_id, ownerUserId: data.owner_user_id,
name: data.name, name: data.name,
} };
} }
/** /**
* @link https://discord.com/developers/docs/resources/application#application-object * @link https://discord.com/developers/docs/resources/application#application-object
*/ */
export class Application implements Model { export class Application implements Model {
constructor(session: Session, data: DiscordApplication) {
constructor(session: Session, data: DiscordApplication) { this.id = data.id;
this.id = data.id; this.session = session;
this.session = session;
this.name = data.name;
this.name = data.name; this.icon = data.icon || undefined;
this.icon = data.icon || undefined; this.description = data.description;
this.description = data.description; this.rpcOrigins = data.rpc_origins;
this.rpcOrigins = data.rpc_origins; this.botPublic = data.bot_public;
this.botPublic = data.bot_public; this.botRequireCodeGrant = data.bot_require_code_grant;
this.botRequireCodeGrant = data.bot_require_code_grant; this.termsOfServiceURL = data.terms_of_service_url;
this.termsOfServiceURL = data.terms_of_service_url; this.privacyPolicyURL = data.privacy_policy_url;
this.privacyPolicyURL = data.privacy_policy_url; this.owner = data.owner ? new User(session, data.owner as DiscordUser) : undefined;
this.owner = data.owner ? new User(session, data.owner as DiscordUser) : undefined; this.summary = "";
this.summary = ""; this.verifyKey = data.verify_key;
this.verifyKey = data.verify_key; this.team = data.team ? NewTeam(session, data.team) : undefined;
this.team = data.team ? NewTeam(session, data.team) : undefined; this.guildId = data.guild_id;
this.guildId = data.guild_id; this.coverImage = data.cover_image;
this.coverImage = data.cover_image; this.tags = data.tags;
this.tags = data.tags; this.installParams = data.install_params;
this.installParams = data.install_params; this.customInstallURL = data.custom_install_url;
this.customInstallURL = data.custom_install_url; }
}
readonly session: Session;
readonly session: Session; id: Snowflake;
id: Snowflake; name: string;
name: string; icon?: string;
icon?: string; description: string;
description: string; rpcOrigins?: string[];
rpcOrigins?: string[]; botPublic: boolean;
botPublic: boolean; botRequireCodeGrant: boolean;
botRequireCodeGrant: boolean; termsOfServiceURL?: string;
termsOfServiceURL?: string; privacyPolicyURL?: string;
privacyPolicyURL?: string; owner?: Partial<User>;
owner?: Partial<User>; summary: SummaryDeprecated;
summary: SummaryDeprecated; verifyKey: string;
verifyKey: string; team?: Team;
team?: Team; guildId?: Snowflake;
guildId?: Snowflake; primarySkuId?: Snowflake;
primarySkuId?: Snowflake; slug?: string;
slug?: string; coverImage?: string;
coverImage?: string; flags?: number;
flags?: number; tags?: string[];
tags?: string[]; installParams?: DiscordInstallParams;
installParams?: DiscordInstallParams; customInstallURL?: string;
customInstallURL?: string; }
}
export default Application;
export default Application;

View File

@ -4,9 +4,9 @@ import type { Session } from "../Session.ts";
import { PrivacyLevels } from "./StageInstance.ts"; import { PrivacyLevels } from "./StageInstance.ts";
import type { import type {
DiscordScheduledEvent, DiscordScheduledEvent,
ScheduledEventStatus, DiscordScheduledEventEntityMetadata,
ScheduledEventEntityType, ScheduledEventEntityType,
DiscordScheduledEventEntityMetadata ScheduledEventStatus,
} from "../../discordeno/mod.ts"; } from "../../discordeno/mod.ts";
import User from "./User.ts"; import User from "./User.ts";
@ -45,5 +45,4 @@ export class ScheduledEvent implements Model {
creator?: User; creator?: User;
userCount?: number; userCount?: number;
image?: string; image?: string;
}
}

View File

@ -1,15 +1,15 @@
import type { Session } from "../Session.ts"; import type { Session } from "../Session.ts";
import type { Snowflake } from "../Snowflake.ts"; import type { Snowflake } from "../Snowflake.ts";
import type { import type {
DiscordApplication,
DiscordChannel, DiscordChannel,
DiscordInvite, DiscordInvite,
DiscordInviteCreate,
DiscordMemberWithUser, DiscordMemberWithUser,
DiscordScheduledEventEntityMetadata, DiscordScheduledEventEntityMetadata,
ScheduledEventEntityType, ScheduledEventEntityType,
ScheduledEventPrivacyLevel, ScheduledEventPrivacyLevel,
ScheduledEventStatus, ScheduledEventStatus,
DiscordApplication,
DiscordInviteCreate
} from "../../discordeno/mod.ts"; } from "../../discordeno/mod.ts";
import { TargetTypes } from "../../discordeno/mod.ts"; import { TargetTypes } from "../../discordeno/mod.ts";
import { GuildChannel } from "./channels.ts"; import { GuildChannel } from "./channels.ts";
@ -75,10 +75,12 @@ export function NewInviteCreate(session: Session, invite: DiscordInviteCreate):
maxUses: invite.max_uses, maxUses: invite.max_uses,
targetType: invite.target_type, targetType: invite.target_type,
targetUser: invite.target_user ? new User(session, invite.target_user) : undefined, targetUser: invite.target_user ? new User(session, invite.target_user) : undefined,
targetApplication: invite.target_application ? new Application(session, invite.target_application as DiscordApplication) : undefined, targetApplication: invite.target_application
? new Application(session, invite.target_application as DiscordApplication)
: undefined,
temporary: invite.temporary, temporary: invite.temporary,
uses: invite.uses uses: invite.uses,
} };
} }
/** /**
@ -95,9 +97,11 @@ export class Invite {
this.expiresAt = data.expires_at ? Number.parseInt(data.expires_at) : undefined; this.expiresAt = data.expires_at ? Number.parseInt(data.expires_at) : undefined;
this.inviter = data.inviter ? new User(session, data.inviter) : undefined; this.inviter = data.inviter ? new User(session, data.inviter) : undefined;
this.targetUser = data.target_user ? new User(session, data.target_user) : undefined; this.targetUser = data.target_user ? new User(session, data.target_user) : undefined;
this.targetApplication = data.target_application ? new Application(session, data.target_application as DiscordApplication) : undefined; this.targetApplication = data.target_application
? new Application(session, data.target_application as DiscordApplication)
: undefined;
this.targetType = data.target_type; this.targetType = data.target_type;
if (data.channel) { if (data.channel) {
const guildId = (data.guild && data.guild?.id) ? data.guild.id : ""; const guildId = (data.guild && data.guild?.id) ? data.guild.id : "";
this.channel = new GuildChannel(session, data.channel as DiscordChannel, guildId); this.channel = new GuildChannel(session, data.channel as DiscordChannel, guildId);
@ -157,7 +161,7 @@ export class Invite {
channel?: Partial<GuildChannel>; channel?: Partial<GuildChannel>;
stageInstance?: InviteStageInstance; stageInstance?: InviteStageInstance;
guildScheduledEvent?: InviteScheduledEvent; guildScheduledEvent?: InviteScheduledEvent;
targetApplication?: Partial<Application> targetApplication?: Partial<Application>;
async delete(): Promise<Invite> { async delete(): Promise<Invite> {
await Guild.prototype.deleteInvite.call(this.guild, this.code); await Guild.prototype.deleteInvite.call(this.guild, this.code);