Skip to content

Commit

Permalink
Stage instance caching and gateway events
Browse files Browse the repository at this point in the history
  • Loading branch information
bsian03 committed Aug 17, 2021
1 parent 0de18ca commit 239f48d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,9 @@ declare namespace Eris {
permissions: Permission;
position: number;
}
interface OldStageInstance {
topic: string;
}
interface OldThread {
name: string;
rateLimitPerUser: number;
Expand Down Expand Up @@ -553,6 +556,8 @@ declare namespace Eris {
event: "relationshipUpdate",
listener: (relationship: Relationship, oldRelationship: { type: number }) => void
): T;
(event: "stageInstanceCreate" | "stageInstanceDelete", listener: (stageInstance: StageInstance) => void): T;
(event: "stageInstanceUpdate", listener: (stageInstance: StageInstance, oldStageInstance: OldStageInstance | null) => void): T;
(event: "threadCreate" | "threadDelete", listener: (channel: AnyThreadChannel) => void): T;
(event: "threadListSync", listener: (guild: Guild, deletedThreads: (AnyThreadChannel | Uncached)[], activeThreads: AnyThreadChannel[], joinedThreadsMember: ThreadMember[]) => void): T;
(event: "threadMembersUpdate", listener: (channel: AnyThreadChannel, removedMembers: (ThreadMember | Uncached)[], addedMembers: ThreadMember[]) => void): T;
Expand Down Expand Up @@ -2080,6 +2085,7 @@ declare namespace Eris {
shard: Shard;
splash: string | null;
splashURL: string | null;
stageInstances: Collection<StageInstance>;
systemChannelFlags: number;
systemChannelID: string | null;
threads: Collection<ThreadChannel>;
Expand Down
52 changes: 52 additions & 0 deletions lib/gateway/Shard.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const User = require("../structures/User");
const Invite = require("../structures/Invite");
const Constants = require("../Constants");
const ThreadChannel = require("../structures/ThreadChannel");
const StageInstance = require("../structures/StageInstance");

const WebSocket = typeof window !== "undefined" ? require("../util/BrowserWebSocket") : require("ws");

Expand Down Expand Up @@ -2214,6 +2215,57 @@ class Shard extends EventEmitter {
this.emit("threadMembersUpdate", channel, removedMembers, addedMembers);
break;
}
case "STAGE_INSTANCE_CREATE": {
const guild = this.client.guilds.get(packet.d.guild_id);
if(!guild) {
this.emit("debug", `Missing guild ${packet.d.guild_id} in STAGE_INSTANCE_CREATE`);
break;
}
/**
* Fired when a stage instance is created
* @event Client#stageInstanceCreate
* @prop {StageInstance} stageInstance The stage instance
*/
this.emit("stageInstanceCreate", guild.stageInstances.add(packet.d));
break;
}
case "STAGE_INSTANCE_UPDATE": {
const guild = this.client.guilds.get(packet.d.guild_id);
if(!guild) {
this.emit("stageInstanceUpdate", packet.d, null);
break;
}
const stageInstance = guild.stageInstances.get(packet.d.id);
let oldStageInstance = null;
if(stageInstance) {
oldStageInstance = {
topic: stageInstance.topic
};
}
/**
* Fired when a stage instance is updated
* @event Client#stageInstanceUpdate
* @prop {StageInstance} stageInstance The stage instance
* @prop {Object?} oldStageInstance The old stage instance. If the stage instance was cached, this will be an object with the properties below. Otherwise, it will be null
* @prop {String} oldStageInstance.topic The stage instance topic
*/
this.emit("stageInstanceUpdate", guild.stageInstances.update(packet.d, this.client), oldStageInstance);
break;
}
case "STAGE_INSTANCE_DELETE": {
const guild = this.client.guilds.get(packet.d.guild_id);
if(!guild) {
this.emit("stageInstanceDelete", new StageInstance(packet.d, this.client));
break;
}
/**
* Fired when a stage instance is deleted
* @event Client#stageInstanceDelete
* @prop {StageInstance} stageInstance The deleted stage instance
*/
this.emit("stageInstanceDelete", guild.stageInstances.remove(packet.d) || new StageInstance(packet.d, this.client));
break;
}
case "MESSAGE_ACK": // Ignore these
case "GUILD_INTEGRATIONS_UPDATE":
case "USER_SETTINGS_UPDATE":
Expand Down
10 changes: 10 additions & 0 deletions lib/structures/Guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const Role = require("./Role");
const VoiceState = require("./VoiceState");
const Permission = require("./Permission");
const {Permissions} = require("../Constants");
const StageInstance = require("./StageInstance");
const ThreadChannel = require("./ThreadChannel");

/**
Expand Down Expand Up @@ -61,6 +62,7 @@ const ThreadChannel = require("./ThreadChannel");
* @prop {Shard} shard The Shard that owns the guild
* @prop {String?} splash The hash of the guild splash image, or null if no splash (VIP only)
* @prop {String?} splashURL The URL of the guild's splash image
* @prop {Collection<StageInstance>} stageInstances Collection of stage instances in the guild
* @prop {Number} systemChannelFlags The flags for the system channel
* @prop {String?} systemChannelID The ID of the default channel for system messages (built-in join messages and boost messages)
* @prop {Collection<ThreadChannel>} threads Collection of threads that the current user has permission to view
Expand All @@ -85,6 +87,7 @@ class Guild extends Base {
this.channels = new Collection(GuildChannel);
this.threads = new Collection(ThreadChannel);
this.members = new Collection(Member);
this.stageInstances = new Collection(StageInstance);
this.memberCount = data.member_count;
this.roles = new Collection(Role);
this.applicationID = data.application_id;
Expand Down Expand Up @@ -152,6 +155,13 @@ class Guild extends Base {
member.id = member.user.id;
this.members.add(member, this);
}
}

if(data.stage_instances) {
for(const stageInstance of data.stage_instances) {
stageInstance.guild_id = this.id;
this.stageInstances.add(stageInstance, client);
}
}

if(data.presences) {
Expand Down

0 comments on commit 239f48d

Please sign in to comment.