feat: Stages, Scheduled events (#44)

Stages Intance and Guild Scheduled Events
This commit is contained in:
Marcos Susaña 2022-07-08 23:14:50 -04:00 committed by GitHub
commit 6ad22bb44d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 233 additions and 122 deletions

View File

@ -16,6 +16,8 @@ import type {
DiscordIntegration,
DiscordIntegrationDelete,
DiscordInteraction,
DiscordInviteCreate,
DiscordInviteDelete,
DiscordMemberWithUser,
DiscordMessage,
DiscordMessageDelete,
@ -25,24 +27,26 @@ import type {
DiscordMessageReactionRemoveEmoji,
DiscordReady,
DiscordRole,
DiscordScheduledEvent,
DiscordScheduledEventUserAdd,
DiscordScheduledEventUserRemove,
// DiscordThreadMemberUpdate,
// DiscordThreadMembersUpdate,
DiscordThreadListSync,
DiscordTypingStart,
DiscordUser,
DiscordWebhookUpdate,
DiscordInviteCreate,
DiscordInviteDelete
} from "../discordeno/mod.ts";
import type { Snowflake } from "./Snowflake.ts";
import type { Session } from "./Session.ts";
import type { Interaction } from "./structures/interactions/InteractionFactory.ts";
import { AutoModerationRule } from "./structures/AutoModerationRule.ts";
import { AutoModerationExecution } from "./structures/AutoModerationExecution.ts";
import { type Channel, ChannelFactory, GuildChannel, ThreadChannel } from "./structures/channels.ts";
import { type DiscordStageInstance, StageInstance } from "./structures/StageInstance.ts";
import { ScheduledEvent } from "./structures/GuildScheduledEvent.ts";
import ThreadMember from "./structures/ThreadMember.ts";
import Member from "./structures/Member.ts";
@ -51,7 +55,7 @@ import User from "./structures/User.ts";
import Integration from "./structures/Integration.ts";
import Guild from "./structures/guilds/Guild.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 Handler<T extends unknown[]> = (...args: T) => unknown;
@ -280,11 +284,59 @@ export const MESSAGE_REACTION_REMOVE_EMOJI: RawHandler<DiscordMessageReactionRem
export const INVITE_CREATE: RawHandler<DiscordInviteCreate> = (session, _shardId, invite) => {
session.emit("inviteCreate", NewInviteCreate(session, invite));
}
};
export const INVITE_DELETE: RawHandler<DiscordInviteDelete> = (session, _shardId, data) => {
session.emit("inviteDelete", { channelId: data.channel_id, guildId: data.guild_id, code: data.code });
}
};
export const STAGE_INSTANCE_CREATE: RawHandler<DiscordStageInstance> = (session, _shardId, payload) => {
session.emit("stageInstanceCreate", new StageInstance(session, payload));
};
export const STAGE_INSTANCE_UPDATE: RawHandler<DiscordStageInstance> = (session, _shardId, payload) => {
session.emit("stageInstanceUpdate", new StageInstance(session, payload));
};
export const STAGE_INSTANCE_DELETE: RawHandler<DiscordStageInstance> = (session, _shardId, payload) => {
session.emit("stageInstanceDelete", new StageInstance(session, payload));
};
export const GUILD_SCHEDULED_EVENT_CREATE: RawHandler<DiscordScheduledEvent> = (session, _shardId, payload) => {
session.emit("guildScheduledEventCreate", new ScheduledEvent(session, payload));
};
export const GUILD_SCHEDULED_EVENT_UPDATE: RawHandler<DiscordScheduledEvent> = (session, _shardId, payload) => {
session.emit("guildScheduledEventUpdate", new ScheduledEvent(session, payload));
};
export const GUILD_SCHEDULED_EVENT_DELETE: RawHandler<DiscordScheduledEvent> = (session, _shardId, 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) => {
session.emit("raw", data, shardId);
@ -337,6 +389,14 @@ export interface Events {
"autoModerationRuleUpdate": Handler<[AutoModerationRule]>;
"autoModerationRuleDelete": Handler<[AutoModerationRule]>;
"autoModerationActionExecution":Handler<[AutoModerationExecution]>
"stageInstanceCreate": Handler<[StageInstance]>;
"stageInstanceUpdate": Handler<[StageInstance]>;
"stageInstanceDelete": Handler<[StageInstance]>;
"guildScheduledEventCreate": Handler<[ScheduledEvent]>;
"guildScheduledEventUpdate": 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]>;
"webhooksUpdate": Handler<[{ guildId: Snowflake, channelId: Snowflake }]>;
"userUpdate": Handler<[User]>;

View File

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

View File

@ -0,0 +1,48 @@
import type { Model } from "./Base.ts";
import type { Snowflake } from "../Snowflake.ts";
import type { Session } from "../Session.ts";
import { PrivacyLevels } from "./StageInstance.ts";
import type {
DiscordScheduledEvent,
DiscordScheduledEventEntityMetadata,
ScheduledEventEntityType,
ScheduledEventStatus,
} from "../../discordeno/mod.ts";
import User from "./User.ts";
export class ScheduledEvent implements Model {
constructor(session: Session, data: DiscordScheduledEvent) {
this.session = session;
this.id = data.id;
this.guildId = data.guild_id;
this.channelId = data.channel_id;
this.creatorId = data.creator_id ? data.creator_id : undefined;
this.name = data.name;
this.description = data.description;
this.scheduledStartTime = data.scheduled_start_time;
this.scheduledEndTime = data.scheduled_end_time;
this.privacyLevel = PrivacyLevels.GuildOnly;
this.status = data.status;
this.entityType = data.entity_type;
this.entityMetadata = data.entity_metadata ? data.entity_metadata : undefined;
this.creator = data.creator ? new User(session, data.creator) : undefined;
this.userCount = data.user_count;
this.image = data.image ? data.image : undefined;
}
session: Session;
id: Snowflake;
guildId: Snowflake;
channelId: Snowflake | null;
creatorId?: Snowflake;
name: string;
description?: string;
scheduledStartTime: string;
scheduledEndTime: string | null;
privacyLevel: PrivacyLevels;
status: ScheduledEventStatus;
entityType: ScheduledEventEntityType;
entityMetadata?: DiscordScheduledEventEntityMetadata;
creator?: User;
userCount?: number;
image?: string;
}

View File

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

View File

@ -4,7 +4,7 @@ import type { Snowflake } from "../Snowflake.ts";
import type { DiscordStageInstance as DiscordAutoClosingStageInstance } from "../../discordeno/mod.ts";
import * as Routes from "../Routes.ts";
interface DiscordStageInstance extends DiscordAutoClosingStageInstance {
export interface DiscordStageInstance extends DiscordAutoClosingStageInstance {
privacy_level: PrivacyLevels;
discoverable_disabled: boolean;
guild_scheduled_event_id: Snowflake;