Skip to content

Commit

Permalink
add validation for structures
Browse files Browse the repository at this point in the history
  • Loading branch information
Starman3787 committed Aug 22, 2024
1 parent a5f4618 commit 58b8d71
Show file tree
Hide file tree
Showing 25 changed files with 233 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/structures/Attachment.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Client from "../Client.js";
import { CDN_BASE_URL, TO_JSON_TYPES_ENUM } from "../constants.js";
import util from "util";

Expand All @@ -21,6 +22,13 @@ class Attachment {
* @param {String} options.channelId The ID of the channel that this attachment belongs to.
*/
constructor(client, data, { channelId } = {}) {
if (!(client instanceof Client))
throw new TypeError("GLUON: Client must be an instance of Client");
if (typeof data !== "object")
throw new TypeError("GLUON: Data must be an object");
if (typeof channelId !== "string")
throw new TypeError("GLUON: Channel ID must be a string");

/**
* The client instance.
* @type {Client}
Expand Down Expand Up @@ -65,10 +73,10 @@ class Attachment {

/**
* The channel that this attachment belongs to.
* @type {BigInt?}
* @type {BigInt}
* @private
*/
if (channelId) this.#_channel_id = BigInt(channelId);
this.#_channel_id = BigInt(channelId);
}

/**
Expand Down
15 changes: 14 additions & 1 deletion src/structures/AuditLog.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Client from "../Client.js";
import { TO_JSON_TYPES_ENUM } from "../constants.js";
import User from "./User.js";
import util from "util";
Expand Down Expand Up @@ -30,10 +31,22 @@ class AuditLog {
* @param {Client} client The client instance.
* @param {Object} data Audit log data from Discord.
* @param {Object} options Additional options for this structure.
* @param {Array<Object>} options.users Resolved users who are involved with the audit log entries.
* @param {Array<Object>?} options.users Resolved users who are involved with the audit log entries.
* @param {String} options.guildId The ID of the guild that this audit log belongs to.
*/
constructor(client, data, { users, guildId } = {}) {
if (!(client instanceof Client))
throw new TypeError("GLUON: Client must be an instance of Client");
if (typeof data !== "object")
throw new TypeError("GLUON: Data must be an object");
if (
users &&
(!Array.isArray(users) || !users.every((u) => u instanceof Object))
)
throw new TypeError("GLUON: Users must be an array of objects");
if (typeof guildId !== "string")
throw new TypeError("GLUON: Guild ID must be a string");

/**
* The client instance.
* @type {Client}
Expand Down
10 changes: 10 additions & 0 deletions src/structures/ButtonClick.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Client from "../Client.js";
import { TO_JSON_TYPES_ENUM } from "../constants.js";
import Interaction from "./Interaction.js";
import Message from "./Message.js";
Expand All @@ -23,6 +24,15 @@ class ButtonClick extends Interaction {
constructor(client, data, { guildId, channelId } = {}) {
super(client, data);

if (!(client instanceof Client))
throw new TypeError("GLUON: Client must be an instance of Client");
if (typeof data !== "object")
throw new TypeError("GLUON: Data must be an object");
if (typeof guildId !== "string")
throw new TypeError("GLUON: Guild ID must be a string");
if (typeof channelId !== "string")
throw new TypeError("GLUON: Channel ID must be a string");

this.#_client = client;

/**
Expand Down
10 changes: 10 additions & 0 deletions src/structures/CategoryChannel.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Client from "../Client.js";
import { TO_JSON_TYPES_ENUM } from "../constants.js";
import Channel from "./Channel.js";
import PermissionOverwrite from "./PermissionOverwrite.js";
Expand All @@ -22,6 +23,15 @@ class CategoryChannel {
* @param {Boolean} options.nocache Whether this channel should be cached or not.
*/
constructor(client, data, { guildId, nocache = false } = { nocache: false }) {
if (!(client instanceof Client))
throw new TypeError("GLUON: Client must be an instance of Client");
if (typeof data !== "object")
throw new TypeError("GLUON: Data must be an object");
if (typeof guildId !== "string")
throw new TypeError("GLUON: Guild ID must be a string");
if (typeof nocache !== "boolean")
throw new TypeError("GLUON: No cache must be a boolean");

/**
* The client instance.
* @type {Client}
Expand Down
7 changes: 7 additions & 0 deletions src/structures/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ class Channel {
* @see {@link https://discord.com/developers/docs/resources/channel#channel-object-channel-structure}
*/
constructor(client, data, { guildId } = {}) {
if (!(client instanceof Client))
throw new TypeError("GLUON: Client must be an instance of Client");
if (typeof data !== "object")
throw new TypeError("GLUON: Data must be an object");
if (typeof guildId !== "string")
throw new TypeError("GLUON: Guild ID must be a string");

/**
* The client instance.
* @type {Client}
Expand Down
10 changes: 10 additions & 0 deletions src/structures/Emoji.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Client from "../Client.js";
import { CDN_BASE_URL, TO_JSON_TYPES_ENUM } from "../constants.js";
import GluonCacheOptions from "../managers/GluonCacheOptions.js";
import GuildCacheOptions from "../managers/GuildCacheOptions.js";
Expand All @@ -22,6 +23,15 @@ class Emoji {
* @param {Boolean?} options.nocache Whether this emoji should be cached or not.
*/
constructor(client, data, { guildId, nocache = false } = { nocache: false }) {
if (!(client instanceof Client))
throw new TypeError("GLUON: Client must be an instance of Client");
if (typeof data !== "object")
throw new TypeError("GLUON: Data must be an object");
if (typeof guildId !== "string")
throw new TypeError("GLUON: Guild ID must be a string");
if (typeof nocache !== "boolean")
throw new TypeError("GLUON: No cache must be a boolean");

/**
* The client instance.
* @type {Client}
Expand Down
7 changes: 7 additions & 0 deletions src/structures/Guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ class Guild {
* @see {@link https://discord.com/developers/docs/resources/guild#guild-object}
*/
constructor(client, data, { nocache = false } = { nocache: false }) {
if (!(client instanceof Client))
throw new TypeError("GLUON: Client must be an instance of Client");
if (typeof data !== "object")
throw new TypeError("GLUON: Data must be an object");
if (typeof nocache !== "boolean")
throw new TypeError("GLUON: No cache must be a boolean");

/**
* The client instance.
* @type {Client}
Expand Down
6 changes: 6 additions & 0 deletions src/structures/Interaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import TextInput from "../util/builder/textInputBuilder.js";
import { TO_JSON_TYPES_ENUM } from "../constants.js";
import util from "util";
import Message from "./Message.js";
import Client from "../Client.js";

/**
* Represents an interaction received over the gateway.
Expand All @@ -23,6 +24,11 @@ class Interaction {
* @param {Object} data The interaction data from Discord.
*/
constructor(client, data) {
if (!(client instanceof Client))
throw new TypeError("GLUON: Client must be an instance of Client");
if (typeof data !== "object")
throw new TypeError("GLUON: Data must be an object");

/**
* The client instance.
* @type {Client}
Expand Down
10 changes: 10 additions & 0 deletions src/structures/Invite.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Client from "../Client.js";
import { INVITE_BASE_URL, TO_JSON_TYPES_ENUM } from "../constants.js";
import GluonCacheOptions from "../managers/GluonCacheOptions.js";
import GuildCacheOptions from "../managers/GuildCacheOptions.js";
Expand Down Expand Up @@ -26,6 +27,15 @@ class Invite {
* @see {@link https://discord.com/developers/docs/resources/invite#invite-object-invite-structure}
*/
constructor(client, data, { guildId, nocache = false } = { nocache: false }) {
if (!(client instanceof Client))
throw new TypeError("GLUON: Client must be an instance of Client");
if (typeof data !== "object")
throw new TypeError("GLUON: Data must be an object");
if (typeof guildId !== "string")
throw new TypeError("GLUON: Guild ID must be a string");
if (typeof nocache !== "boolean")
throw new TypeError("GLUON: No cache must be a boolean");

/**
* The client instance.
* @type {Client}
Expand Down
13 changes: 13 additions & 0 deletions src/structures/Member.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ class Member {
nocache: false,
},
) {
if (!(client instanceof Client))
throw new TypeError("GLUON: Client must be an instance of Client");
if (typeof data !== "object")
throw new TypeError("GLUON: Data must be an object");
if (typeof guildId !== "string")
throw new TypeError("GLUON: Guild ID must be a string");
if (typeof userId !== "string")
throw new TypeError("GLUON: User ID must be a string");
if (typeof user !== "undefined" && typeof user !== "object")
throw new TypeError("GLUON: User must be an object");
if (typeof nocache !== "boolean")
throw new TypeError("GLUON: No cache must be a boolean");

/**
* The client instance.
* @type {Client}
Expand Down
13 changes: 13 additions & 0 deletions src/structures/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ class Message {
ignoreExisting: false,
},
) {
if (!(client instanceof Client))
throw new TypeError("GLUON: Client must be an instance of Client");
if (typeof data !== "object")
throw new TypeError("GLUON: Data must be an object");
if (typeof channelId !== "string")
throw new TypeError("GLUON: Channel ID must be a string");
if (typeof guildId !== "string")
throw new TypeError("GLUON: Guild ID must be a string");
if (typeof nocache !== "boolean")
throw new TypeError("GLUON: No cache must be a boolean");
if (typeof ignoreExisting !== "boolean")
throw new TypeError("GLUON: Ignore existing must be a boolean");

/**
* The client instance.
* @type {Client}
Expand Down
6 changes: 6 additions & 0 deletions src/structures/ModalResponse.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Client from "../Client.js";
import { TO_JSON_TYPES_ENUM } from "../constants.js";
import Interaction from "./Interaction.js";
import util from "util";
Expand All @@ -17,6 +18,11 @@ class ModalResponse extends Interaction {
constructor(client, data) {
super(client, data);

if (!(client instanceof Client))
throw new TypeError("GLUON: Client must be an instance of Client");
if (typeof data !== "object")
throw new TypeError("GLUON: Data must be an object");

/**
* The entered modal values.
* @type {Array<Object>}
Expand Down
15 changes: 15 additions & 0 deletions src/structures/OptionSelect.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Client from "../Client.js";
import { TO_JSON_TYPES_ENUM } from "../constants.js";
import Interaction from "./Interaction.js";
import Message from "./Message.js";
Expand All @@ -24,6 +25,20 @@ class OptionSelect extends Interaction {
constructor(client, data, { channelId, guildId }) {
super(client, data);

if (!(client instanceof Client))
throw new TypeError("GLUON: Client must be an instance of Client");
if (typeof data !== "object")
throw new TypeError("GLUON: Data must be an object");
if (typeof channelId !== "string")
throw new TypeError("GLUON: Channel ID must be a string");
if (typeof guildId !== "string")
throw new TypeError("GLUON: Guild ID must be a string");

/**
* The client instance.
* @type {Client}
* @private
*/
this.#_client = client;

/**
Expand Down
6 changes: 6 additions & 0 deletions src/structures/PermissionOverwrite.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Client from "../Client.js";
import { TO_JSON_TYPES_ENUM } from "../constants.js";
import util from "util";

Expand All @@ -13,6 +14,11 @@ class PermissionOverwrite {
* @param {Object} data The raw permission overwrite data.
*/
constructor(client, data) {
if (!(client instanceof Client))
throw new TypeError("GLUON: Client must be an instance of Client");
if (typeof data !== "object")
throw new TypeError("GLUON: Data must be an object");

/**
* The client instance.
* @type {Client}
Expand Down
8 changes: 8 additions & 0 deletions src/structures/Poll.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Client from "../Client.js";
import { TO_JSON_TYPES_ENUM } from "../constants.js";
import MessagePollManager from "../managers/MessagePollManager.js";
import Emoji from "./Emoji.js";
Expand All @@ -20,6 +21,13 @@ class Poll {
* @param {String} options.guildId The ID of the guild that this poll belongs to.
*/
constructor(client, data, { guildId } = {}) {
if (!(client instanceof Client))
throw new TypeError("GLUON: Client must be an instance of Client");
if (typeof data !== "object")
throw new TypeError("GLUON: Data must be an object");
if (typeof guildId !== "string")
throw new TypeError("GLUON: Guild ID must be a string");

/**
* The client instance.
* @type {Client}
Expand Down
8 changes: 8 additions & 0 deletions src/structures/Reaction.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Client from "../Client.js";
import { TO_JSON_TYPES_ENUM } from "../constants.js";
import Emoji from "./Emoji.js";
import util from "util";
Expand All @@ -21,6 +22,13 @@ class Reaction {
* @see {@link https://discord.com/developers/docs/resources/channel#reaction-object-reaction-structure}
*/
constructor(client, data, { guildId } = {}) {
if (!(client instanceof Client))
throw new TypeError("GLUON: Client must be an instance of Client");
if (typeof data !== "object")
throw new TypeError("GLUON: Data must be an object");
if (typeof guildId !== "string")
throw new TypeError("GLUON: Guild ID must be a string");

/**
* The client instance.
* @type {Client}
Expand Down
10 changes: 10 additions & 0 deletions src/structures/Role.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Client from "../Client.js";
import { CDN_BASE_URL, TO_JSON_TYPES_ENUM } from "../constants.js";
import GluonCacheOptions from "../managers/GluonCacheOptions.js";
import GuildCacheOptions from "../managers/GuildCacheOptions.js";
Expand Down Expand Up @@ -27,6 +28,15 @@ class Role {
* @see {@link https://discord.com/developers/docs/topics/permissions#role-object-role-structure}
*/
constructor(client, data, { guildId, nocache = false } = { nocache: false }) {
if (!(client instanceof Client))
throw new TypeError("GLUON: Client must be an instance of Client");
if (typeof data !== "object")
throw new TypeError("GLUON: Data must be an object");
if (typeof guildId !== "string")
throw new TypeError("GLUON: Guild ID must be a string");
if (typeof nocache !== "boolean")
throw new TypeError("GLUON: No cache must be a boolean");

/**
* The client instance.
* @type {Client}
Expand Down
10 changes: 10 additions & 0 deletions src/structures/ScheduledEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CDN_BASE_URL, TO_JSON_TYPES_ENUM } from "../constants.js";
import GluonCacheOptions from "../managers/GluonCacheOptions.js";
import GuildCacheOptions from "../managers/GuildCacheOptions.js";
import util from "util";
import Client from "../Client.js";

/**
* Represents an scheduled event.
Expand Down Expand Up @@ -31,6 +32,15 @@ class ScheduledEvent {
* @param {Boolean?} options.nocache Whether this event should be cached or not.
*/
constructor(client, data, { guildId, nocache = false } = { nocache: false }) {
if (!(client instanceof Client))
throw new TypeError("GLUON: Client must be an instance of Client");
if (typeof data !== "object")
throw new TypeError("GLUON: Data must be an object");
if (typeof guildId !== "string")
throw new TypeError("GLUON: Guild ID must be a string");
if (typeof nocache !== "boolean")
throw new TypeError("GLUON: No cache must be a boolean");

/**
* The client instance.
* @type {Client}
Expand Down
6 changes: 6 additions & 0 deletions src/structures/SlashCommand.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Client from "../Client.js";
import { TO_JSON_TYPES_ENUM } from "../constants.js";
import Interaction from "./Interaction.js";
import util from "util";
Expand All @@ -17,6 +18,11 @@ class SlashCommand extends Interaction {
constructor(client, data) {
super(client, data);

if (!(client instanceof Client))
throw new TypeError("GLUON: Client must be an instance of Client");
if (typeof data !== "object")
throw new TypeError("GLUON: Data must be an object");

/**
* Raw slash command data from discord.
* @type {Object?}
Expand Down
Loading

0 comments on commit 58b8d71

Please sign in to comment.