diff --git a/src/constants.js b/src/constants.js index 6fcb9f6d..aba7e355 100644 --- a/src/constants.js +++ b/src/constants.js @@ -1112,4 +1112,72 @@ export const TEST_DATA = { request_to_speak_timestamp: "2021-03-31T18:45:31.297561+00:00", joined: 1722533062, }, + SCHEDULED_EVENT: { + id: "123409989012345678", + guild_id: "619434557472505857", + channel_id: "333456789012345678", + creator_id: "301655085954367490", + name: "test event", + description: "test description", + scheduled_start_time: "2021-01-01T00:00:00.000Z", + scheduled_end_time: "2021-01-02T01:00:00.000Z", + privacy_level: 1, + status: 1, + entity_type: 2, + entity_id: "333456789012345678", + entity_metadata: { + location: undefined, + }, + creator: { + id: "301655085954367490", + username: "test", + discriminator: "0001", + avatar: null, + bot: false, + system: false, + mfa_enabled: false, + locale: "en-US", + verified: false, + email: null, + flags: 0, + premium_type: 0, + public_flags: 0, + }, + user_count: 10, + image: "000000000000000000000000deadbeef", + }, + SCHEDULED_EVENT_EXTERNAL: { + id: "123409989012345678", + guild_id: "619434557472505857", + channel_id: "333456789012345678", + creator_id: "301655085954367490", + name: "test event", + description: "test description", + scheduled_start_time: "2021-01-01T00:00:00.000Z", + scheduled_end_time: "2021-01-02T01:00:00.000Z", + privacy_level: 1, + status: 1, + entity_type: 3, + entity_id: "333456789012345678", + entity_metadata: { + location: "test location", + }, + creator: { + id: "301655085954367490", + username: "test", + discriminator: "0001", + avatar: null, + bot: false, + system: false, + mfa_enabled: false, + locale: "en-US", + verified: false, + email: null, + flags: 0, + premium_type: 0, + public_flags: 0, + }, + user_count: 10, + image: "000000000000000000000000deadbeef", + }, }; diff --git a/src/structures/ScheduledEvent.js b/src/structures/ScheduledEvent.js index 947d7b06..fb8b6b25 100644 --- a/src/structures/ScheduledEvent.js +++ b/src/structures/ScheduledEvent.js @@ -18,6 +18,7 @@ class ScheduledEvent { #user_count; #_attributes; #location; + #description; /** * * @param {Client} client The client instance. @@ -98,6 +99,8 @@ class ScheduledEvent { */ this.#_image = data.image ? BigInt(`0x${data.image}`) : null; + if (data.description) this.#description = data.description; + /** * The number of users who have signed up for the event. * @type {Number} @@ -219,6 +222,16 @@ class ScheduledEvent { return this.#creator; } + /** + * The description of the event. + * @type {String} + * @readonly + * @public + */ + get description() { + return this.#description; + } + /** * The hash of the event's image, as it was received from Discord. * @readonly @@ -271,6 +284,13 @@ class ScheduledEvent { else return "UNKNOWN"; } + get #rawEntityType() { + if ((this.#_attributes & (0b1 << 0)) == 0b1 << 0) return 1; + else if ((this.#_attributes & (0b1 << 1)) == 0b1 << 1) return 2; + else if ((this.#_attributes & (0b1 << 2)) == 0b1 << 2) return 3; + else return 0; + } + /** * The status of the event. * @readonly @@ -285,6 +305,14 @@ class ScheduledEvent { else return "UNKNOWN"; } + get #rawStatus() { + if ((this.#_attributes & (0b1 << 3)) == 0b1 << 3) return 1; + else if ((this.#_attributes & (0b1 << 4)) == 0b1 << 4) return 2; + else if ((this.#_attributes & (0b1 << 5)) == 0b1 << 5) return 3; + else if ((this.#_attributes & (0b1 << 6)) == 0b1 << 6) return 4; + else return 0; + } + /** * The guild that this event belongs to. * @type {Guild?} @@ -352,6 +380,7 @@ class ScheduledEvent { id: this.id, guild_id: this.guildId, name: this.name, + description: this.description, creator_id: this.creatorId ?? undefined, creator: this.creator, scheduled_start_time: this.scheduledStartTime * 1000, @@ -360,9 +389,11 @@ class ScheduledEvent { : undefined, image: this.#_originalImageHash, user_count: this.userCount, - entity_type: this.entityType, - status: this.status, - location: this.location, + entity_type: this.#rawEntityType, + status: this.#rawStatus, + entity_metadata: { + location: this.location, + }, }; } } diff --git a/test/structures/ScheduledEvent.js b/test/structures/ScheduledEvent.js new file mode 100644 index 00000000..2239580d --- /dev/null +++ b/test/structures/ScheduledEvent.js @@ -0,0 +1,356 @@ +import { expect } from "chai"; +import { TEST_DATA } from "../../src/constants.js"; +import ScheduledEvent from "../../src/structures/ScheduledEvent.js"; +import GuildManager from "../../src/managers/GuildManager.js"; +import Guild from "../../src/structures/Guild.js"; +import User from "../../src/structures/User.js"; + +describe("ScheduledEvent", function () { + context("check import", function () { + it("should be a function", function () { + expect(ScheduledEvent).to.be.a("function"); + }); + }); + + context("check structure", function () { + it("should have the correct structure", function () { + const client = { cacheGuilds: true }; + client.guilds = new GuildManager(client); + new Guild(client, TEST_DATA.GUILD); + client.user = new User(client, TEST_DATA.CLIENT_USER); + const scheduledEvent = new ScheduledEvent( + client, + TEST_DATA.SCHEDULED_EVENT, + { guild_id: TEST_DATA.GUILD_ID }, + ); + expect(scheduledEvent).to.have.property("id"); + expect(scheduledEvent).to.have.property("guildId"); + expect(scheduledEvent).to.have.property("scheduledStartTime"); + expect(scheduledEvent).to.have.property("scheduledEndTime"); + expect(scheduledEvent).to.have.property("userCount"); + expect(scheduledEvent).to.have.property("location"); + expect(scheduledEvent).to.have.property("toString"); + }); + }); + + context("check id", function () { + it("should have the correct id", function () { + const client = { cacheGuilds: true }; + client.guilds = new GuildManager(client); + new Guild(client, TEST_DATA.GUILD); + client.user = new User(client, TEST_DATA.CLIENT_USER); + const scheduledEvent = new ScheduledEvent( + client, + TEST_DATA.SCHEDULED_EVENT, + { guild_id: TEST_DATA.GUILD_ID }, + ); + expect(scheduledEvent.id).to.equal(TEST_DATA.SCHEDULED_EVENT.id); + }); + }); + + context("check guildId", function () { + it("should have the correct guildId", function () { + const client = { cacheGuilds: true }; + client.guilds = new GuildManager(client); + new Guild(client, TEST_DATA.GUILD); + client.user = new User(client, TEST_DATA.CLIENT_USER); + const scheduledEvent = new ScheduledEvent( + client, + TEST_DATA.SCHEDULED_EVENT, + { guild_id: TEST_DATA.GUILD_ID }, + ); + expect(scheduledEvent.guildId).to.equal(TEST_DATA.GUILD_ID); + }); + }); + + context("check guild", function () { + it("should have the correct guild", function () { + const client = { cacheGuilds: true }; + client.guilds = new GuildManager(client); + new Guild(client, TEST_DATA.GUILD); + client.user = new User(client, TEST_DATA.CLIENT_USER); + const scheduledEvent = new ScheduledEvent( + client, + TEST_DATA.SCHEDULED_EVENT, + { guild_id: TEST_DATA.GUILD_ID }, + ); + expect(scheduledEvent.guild).to.be.an.instanceOf(Guild); + }); + }); + + context("check name", function () { + it("should have the correct name", function () { + const client = { cacheGuilds: true }; + client.guilds = new GuildManager(client); + new Guild(client, TEST_DATA.GUILD); + client.user = new User(client, TEST_DATA.CLIENT_USER); + const scheduledEvent = new ScheduledEvent( + client, + TEST_DATA.SCHEDULED_EVENT, + { guild_id: TEST_DATA.GUILD_ID }, + ); + expect(scheduledEvent.name).to.equal(TEST_DATA.SCHEDULED_EVENT.name); + }); + }); + + context("check creatorId", function () { + it("should have the correct creatorId", function () { + const client = { cacheGuilds: true }; + client.guilds = new GuildManager(client); + new Guild(client, TEST_DATA.GUILD); + client.user = new User(client, TEST_DATA.CLIENT_USER); + const scheduledEvent = new ScheduledEvent( + client, + TEST_DATA.SCHEDULED_EVENT, + { guild_id: TEST_DATA.GUILD_ID }, + ); + expect(scheduledEvent.creatorId).to.equal( + TEST_DATA.SCHEDULED_EVENT.creator_id, + ); + }); + }); + + context("check creator", function () { + it("should have the correct creator", function () { + const client = { cacheGuilds: true }; + client.guilds = new GuildManager(client); + new Guild(client, TEST_DATA.GUILD); + client.user = new User(client, TEST_DATA.USER); + const scheduledEvent = new ScheduledEvent( + client, + TEST_DATA.SCHEDULED_EVENT, + { guild_id: TEST_DATA.GUILD_ID }, + ); + expect(scheduledEvent.creator).to.be.an.instanceOf(User); + expect(scheduledEvent.creator.id).to.be.equal( + TEST_DATA.SCHEDULED_EVENT.creator.id, + ); + }); + }); + + context("check displayImageURL", function () { + it("should have the correct displayImageURL", function () { + const client = { cacheGuilds: true }; + client.guilds = new GuildManager(client); + new Guild(client, TEST_DATA.GUILD); + client.user = new User(client, TEST_DATA.USER); + const scheduledEvent = new ScheduledEvent( + client, + TEST_DATA.SCHEDULED_EVENT, + { guild_id: TEST_DATA.GUILD_ID }, + ); + expect(scheduledEvent.displayImageURL).to.equal( + `https://cdn.discordapp.com/guild-events/123409989012345678/${TEST_DATA.SCHEDULED_EVENT.image}.png`, + ); + }); + }); + + context("check description", function () { + it("should have the correct description", function () { + const client = { cacheGuilds: true }; + client.guilds = new GuildManager(client); + new Guild(client, TEST_DATA.GUILD); + client.user = new User(client, TEST_DATA.USER); + const scheduledEvent = new ScheduledEvent( + client, + TEST_DATA.SCHEDULED_EVENT, + { guild_id: TEST_DATA.GUILD_ID }, + ); + expect(scheduledEvent.description).to.equal( + TEST_DATA.SCHEDULED_EVENT.description, + ); + }); + }); + + context("check entityType", function () { + it("should have the correct entityType", function () { + const client = { cacheGuilds: true }; + client.guilds = new GuildManager(client); + new Guild(client, TEST_DATA.GUILD); + client.user = new User(client, TEST_DATA.USER); + const scheduledEvent = new ScheduledEvent( + client, + TEST_DATA.SCHEDULED_EVENT, + { guild_id: TEST_DATA.GUILD_ID }, + ); + expect(scheduledEvent.entityType).to.equal("VOICE"); + }); + it("should have the correct entityType", function () { + const client = { cacheGuilds: true }; + client.guilds = new GuildManager(client); + new Guild(client, TEST_DATA.GUILD); + client.user = new User(client, TEST_DATA.USER); + const scheduledEvent = new ScheduledEvent( + client, + TEST_DATA.SCHEDULED_EVENT_EXTERNAL, + { guild_id: TEST_DATA.GUILD_ID }, + ); + expect(scheduledEvent.entityType).to.equal("EXTERNAL"); + }); + }); + + context("check status", function () { + it("should have the correct status", function () { + const client = { cacheGuilds: true }; + client.guilds = new GuildManager(client); + new Guild(client, TEST_DATA.GUILD); + client.user = new User(client, TEST_DATA.USER); + const scheduledEvent = new ScheduledEvent( + client, + TEST_DATA.SCHEDULED_EVENT, + { guild_id: TEST_DATA.GUILD_ID }, + ); + expect(scheduledEvent.status).to.equal("SCHEDULED"); + }); + }); + + context("check scheduledStartTime", function () { + it("should have the correct scheduledStartTime", function () { + const client = { cacheGuilds: true }; + client.guilds = new GuildManager(client); + new Guild(client, TEST_DATA.GUILD); + client.user = new User(client, TEST_DATA.CLIENT_USER); + const scheduledEvent = new ScheduledEvent( + client, + TEST_DATA.SCHEDULED_EVENT, + { guild_id: TEST_DATA.GUILD_ID }, + ); + expect(scheduledEvent.scheduledStartTime).to.equal( + (new Date(TEST_DATA.SCHEDULED_EVENT.scheduled_start_time).getTime() / + 1000) | + 0, + ); + }); + }); + + context("check scheduledEndTime", function () { + it("should have the correct scheduledEndTime", function () { + const client = { cacheGuilds: true }; + client.guilds = new GuildManager(client); + new Guild(client, TEST_DATA.GUILD); + client.user = new User(client, TEST_DATA.CLIENT_USER); + const scheduledEvent = new ScheduledEvent( + client, + TEST_DATA.SCHEDULED_EVENT, + { guild_id: TEST_DATA.GUILD_ID }, + ); + expect(scheduledEvent.scheduledEndTime).to.equal( + (new Date(TEST_DATA.SCHEDULED_EVENT.scheduled_end_time) / 1000) | 0, + ); + }); + }); + + context("check userCount", function () { + it("should have the correct userCount", function () { + const client = { cacheGuilds: true }; + client.guilds = new GuildManager(client); + new Guild(client, TEST_DATA.GUILD); + client.user = new User(client, TEST_DATA.CLIENT_USER); + const scheduledEvent = new ScheduledEvent( + client, + TEST_DATA.SCHEDULED_EVENT, + { guild_id: TEST_DATA.GUILD_ID }, + ); + expect(scheduledEvent.userCount).to.equal( + TEST_DATA.SCHEDULED_EVENT.user_count, + ); + }); + }); + + context("check location", function () { + it("should have the correct location", function () { + const client = { cacheGuilds: true }; + client.guilds = new GuildManager(client); + new Guild(client, TEST_DATA.GUILD); + client.user = new User(client, TEST_DATA.CLIENT_USER); + const scheduledEvent = new ScheduledEvent( + client, + TEST_DATA.SCHEDULED_EVENT_EXTERNAL, + { guild_id: TEST_DATA.GUILD_ID }, + ); + expect(scheduledEvent.location).to.equal( + TEST_DATA.SCHEDULED_EVENT_EXTERNAL.entity_metadata.location, + ); + }); + }); + + context("check toString", function () { + it("should return a string", function () { + const client = { cacheGuilds: true }; + client.guilds = new GuildManager(client); + new Guild(client, TEST_DATA.GUILD); + client.user = new User(client, TEST_DATA.CLIENT_USER); + const scheduledEvent = new ScheduledEvent( + client, + TEST_DATA.SCHEDULED_EVENT, + { guild_id: TEST_DATA.GUILD_ID }, + ); + expect(scheduledEvent.toString()).to.be.a("string"); + }); + it("should return the correct string", function () { + const client = { cacheGuilds: true }; + client.guilds = new GuildManager(client); + new Guild(client, TEST_DATA.GUILD); + client.user = new User(client, TEST_DATA.CLIENT_USER); + const scheduledEvent = new ScheduledEvent( + client, + TEST_DATA.SCHEDULED_EVENT, + { guild_id: TEST_DATA.GUILD_ID }, + ); + expect(scheduledEvent.toString()).to.equal( + ``, + ); + }); + }); + + context("check toJSON", function () { + it("should return an object", function () { + const client = { cacheGuilds: true }; + client.guilds = new GuildManager(client); + new Guild(client, TEST_DATA.GUILD); + client.user = new User(client, TEST_DATA.CLIENT_USER); + const scheduledEvent = new ScheduledEvent( + client, + TEST_DATA.SCHEDULED_EVENT, + { guild_id: TEST_DATA.GUILD_ID }, + ); + expect(scheduledEvent.toJSON()).to.be.a("object"); + }); + it("should return the correct object", function () { + const client = { cacheGuilds: true }; + client.guilds = new GuildManager(client); + new Guild(client, TEST_DATA.GUILD); + client.user = new User(client, TEST_DATA.CLIENT_USER); + const scheduledEvent = new ScheduledEvent( + client, + TEST_DATA.SCHEDULED_EVENT, + { guild_id: TEST_DATA.GUILD_ID }, + ); + expect(scheduledEvent.toJSON()).to.deep.equal({ + id: TEST_DATA.SCHEDULED_EVENT.id, + creator: {}, + creator_id: TEST_DATA.SCHEDULED_EVENT.creator_id, + description: TEST_DATA.SCHEDULED_EVENT.description, + entity_type: 2, + guild_id: TEST_DATA.GUILD_ID, + image: TEST_DATA.SCHEDULED_EVENT.image, + user_count: TEST_DATA.SCHEDULED_EVENT.user_count, + status: 1, + scheduled_start_time: + ((new Date(TEST_DATA.SCHEDULED_EVENT.scheduled_start_time).getTime() / + 1000) | + 0) * + 1000, + scheduled_end_time: + ((new Date(TEST_DATA.SCHEDULED_EVENT.scheduled_end_time).getTime() / + 1000) | + 0) * + 1000, + name: TEST_DATA.SCHEDULED_EVENT.name, + entity_metadata: { + location: TEST_DATA.SCHEDULED_EVENT.entity_metadata.location, + }, + }); + }); + }); +});