diff --git a/src/Client.js b/src/Client.js index 79205409..9da0c9dc 100644 --- a/src/Client.js +++ b/src/Client.js @@ -420,150 +420,6 @@ class Client extends EventsEmitter { return this.guilds.toJSON(TO_JSON_TYPES_ENUM.CACHE_FORMAT); } - /** - * Posts a webhook with the provided webhook id and token. - * @param {Object} referenceData An object with the webhook id and token. - * @param {String?} content The message to send with the webhook. - * @param {Object?} options Embeds, components and files to attach to the webhook. - * @returns {Promise} - * @public - * @method - * @async - * @throws {TypeError} - */ - async postWebhook( - { id, token }, - content, - { embeds, components, files } = {}, - ) { - if (typeof id !== "string") - throw new TypeError("GLUON: Webhook ID is not a string."); - if (typeof token !== "string") - throw new TypeError("GLUON: Webhook token is not a string."); - - Message.sendValidation(content, { embeds, components, files }); - - const body = {}; - - if (content) body.content = content; - - if (embeds) body.embeds = embeds; - if (components) body.components; - if (files) body.files = files; - - await this.request.makeRequest("postExecuteWebhook", [id, token], body); - } - - /** - * Deletes a webhook. - * @param {String} webhook_id The id of the webhook to delete. - * @returns {Promise} - * @public - * @method - * @async - * @throws {TypeError} - */ - async deleteWebhook(webhook_id) { - if (typeof webhook_id !== "string") - throw new TypeError("GLUON: Webhook ID is not a string."); - await this.request.makeRequest("deleteWebhook", [webhook_id]); - } - - /** - * Bulk deletes channel messages. - * @param {String} channel_id The id of the channel to purge messages in. - * @param {Array} messages An array of message ids to delete. - * @param {Object} options - * @returns {Promise} - * @public - * @method - * @async - * @throws {TypeError} - */ - async purgeChannelMessages(channel_id, messages, { reason } = {}) { - if (typeof channel_id !== "string") - throw new TypeError("GLUON: Channel ID is not a string."); - if ( - !Array.isArray(messages) || - !messages.every((m) => typeof m === "string") - ) - throw new TypeError( - "GLUON: Messages is not an array of message id strings.", - ); - if (typeof reason !== "undefined" && typeof reason !== "string") - throw new TypeError("GLUON: Reason is not a string."); - - const body = {}; - - body.messages = messages; - - if (reason) body["X-Audit-Log-Reason"] = reason; - - await this.request.makeRequest( - "postBulkDeleteMessages", - [channel_id], - body, - ); - } - - /** - * Creates a webhook in the given channel with the name "Gluon". - * @param {String} channel_id The id of the channel to create the webhook in. - * @returns {Promise} - * @public - * @method - * @async - * @throws {TypeError} - */ - createWebhook(channel_id, { name = NAME } = { name: NAME }) { - if (typeof channel_id !== "string") - throw new TypeError("GLUON: Channel ID is not a string."); - - const body = {}; - - body.name = name; - - return this.request.makeRequest("postCreateWebhook", [channel_id], body); - } - - /** - * Modified a webhook with the given webhook id. - * @param {String} webhook_id The id of the webhook to modify. - * @param {Object} options The options to modify the webhook with. - * @returns {Promise} - * @public - * @method - * @async - * @throws {TypeError} - */ - modifyWebhook(webhook_id, { channel_id } = {}) { - if (typeof webhook_id !== "string") - throw new TypeError("GLUON: Webhook ID is not a string."); - if (typeof channel_id !== "string") - throw new TypeError("GLUON: Channel ID is not a string."); - - const body = {}; - - body.channel_id = channel_id; - - return this.request.makeRequest("patchModifyWebhook", [webhook_id], body); - } - - /** - * Fetches a webhook by the webhook's id. - * @param {String} webhook_id The id of the webhook to fetch. - * @returns {Promise} - * @public - * @method - * @async - * @throws {TypeError} - */ - fetchWebhook(webhook_id) { - if (typeof webhook_id !== "string") - throw new TypeError("GLUON: Webhook ID is not a string."); - return this.request.makeRequest("getWebhook", [webhook_id]); - } - /** * Registers commands, overwriting all previous ones. * @param {Array} commands Array of commands to register. @@ -593,49 +449,6 @@ class Client extends EventsEmitter { ); } - /** - * Searches for members via a search query. - * @param {String} guildId The id of the guild to search. - * @param {String} query The search query. - * @returns {Promise?>} The members which match the search query. - * @public - * @method - * @async - * @throws {TypeError} - */ - async search(guildId, query) { - if (typeof guildId !== "string") - throw new TypeError("GLUON: Guild ID is not a string."); - if (typeof query !== "string") - throw new TypeError("GLUON: Query is not a string."); - - const body = {}; - - body.query = query; - - body.limit = 1000; - - const data = await this.request.makeRequest( - "getSearchGuildMembers", - [guildId], - body, - ); - if (data.length !== 0) { - const members = []; - - for (let i = 0; i < data.length; i++) - members.push( - new Member(this, data[i], { - user_id: data[i].user.id, - guild_id: guildId.toString(), - user: data[i].user, - }), - ); - - return members; - } else return null; - } - /** * Sets the bot's status across all shards. * @param {Object} status Status options. diff --git a/src/managers/ChannelMessageManager.js b/src/managers/ChannelMessageManager.js index a255f457..a37ef901 100644 --- a/src/managers/ChannelMessageManager.js +++ b/src/managers/ChannelMessageManager.js @@ -351,6 +351,48 @@ class ChannelMessageManager extends BaseCacheManager { ); return messages; } + + /** + * Bulk deletes channel messages. + * @param {Client} client The client instance. + * @param {String} channelId The id of the channel to purge messages in. + * @param {Array} messages An array of message ids to delete. + * @param {Object} options + * @returns {Promise} + * @public + * @method + * @async + * @throws {TypeError} + * @static + */ + static async purgeChannelMessages( + client, + channelId, + messages, + { reason } = {}, + ) { + if (!(client instanceof Client)) + throw new TypeError("GLUON: Client must be a Client instance."); + if (typeof channelId !== "string") + throw new TypeError("GLUON: Channel ID is not a string."); + if ( + !Array.isArray(messages) || + !messages.every((m) => typeof m === "string") + ) + throw new TypeError( + "GLUON: Messages is not an array of message id strings.", + ); + if (typeof reason !== "undefined" && typeof reason !== "string") + throw new TypeError("GLUON: Reason is not a string."); + + const body = {}; + + body.messages = messages; + + if (reason) body["X-Audit-Log-Reason"] = reason; + + await this.request.makeRequest("postBulkDeleteMessages", [channelId], body); + } } export default ChannelMessageManager; diff --git a/src/managers/GuildMemberManager.js b/src/managers/GuildMemberManager.js index ed3de974..4acad99b 100644 --- a/src/managers/GuildMemberManager.js +++ b/src/managers/GuildMemberManager.js @@ -165,6 +165,51 @@ class GuildMemberManager extends BaseCacheManager { user: data.user, }); } + + /** + * Searches for members via a search query. + * @param {Client} client The client instance. + * @param {String} guildId The id of the guild to search. + * @param {String} query The search query. + * @returns {Promise?>} The members which match the search query. + * @public + * @method + * @async + * @throws {TypeError} + * @static + */ + static async search(client, guildId, query) { + if (typeof guildId !== "string") + throw new TypeError("GLUON: Guild ID is not a string."); + if (typeof query !== "string") + throw new TypeError("GLUON: Query is not a string."); + + const body = {}; + + body.query = query; + + body.limit = 1000; + + const data = await client.request.makeRequest( + "getSearchGuildMembers", + [guildId], + body, + ); + if (data.length !== 0) { + const members = []; + + for (let i = 0; i < data.length; i++) + members.push( + new Member(client, data[i], { + user_id: data[i].user.id, + guild_id: guildId.toString(), + user: data[i].user, + }), + ); + + return members; + } else return null; + } } export default GuildMemberManager; diff --git a/src/structures/Guild.js b/src/structures/Guild.js index 06884973..d4add078 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -1,6 +1,7 @@ import { AUDIT_LOG_TYPES, CDN_BASE_URL, + NAME, PERMISSIONS, TO_JSON_TYPES_ENUM, } from "../constants.js"; @@ -24,6 +25,8 @@ import GuildCacheOptions from "../managers/GuildCacheOptions.js"; import Channel from "./Channel.js"; import GluonCacheOptions from "../managers/GluonCacheOptions.js"; import util from "util"; +import Client from "../Client.js"; +import Message from "./Message.js"; /** * Represents a Discord guild. @@ -1375,6 +1378,139 @@ class Guild { return shouldCacheCount; } + /** + * Deletes a webhook. + * @param {Client} client The client instance. + * @param {String} webhookId The id of the webhook to delete. + * @returns {Promise} + * @public + * @method + * @async + * @throws {TypeError} + * @static + */ + static async deleteWebhook(client, webhookId) { + if (!(client instanceof Client)) + throw new TypeError("GLUON: Client must be a Client instance."); + if (typeof webhookId !== "string") + throw new TypeError("GLUON: Webhook ID is not a string."); + await client.request.makeRequest("deleteWebhook", [webhookId]); + } + + /** + * Creates a webhook in the given channel with the name "Gluon". + * @param {Client} client The client instance. + * @param {String} channelId The id of the channel to create the webhook in. + * @param {Object} options The options for creating the webhook. + * @param {String} options.name The name of the webhook. + * @returns {Promise} + * @public + * @method + * @async + * @throws {TypeError} + * @static + */ + static createWebhook(client, channelId, { name = NAME } = { name: NAME }) { + if (!(client instanceof Client)) + throw new TypeError("GLUON: Client must be a Client instance."); + if (typeof channelId !== "string") + throw new TypeError("GLUON: Channel ID is not a string."); + if (typeof name !== "string") + throw new TypeError("GLUON: Name must be a string."); + + const body = {}; + + body.name = name; + + return client.request.makeRequest("postCreateWebhook", [channelId], body); + } + + /** + * Modified a webhook with the given webhook id. + * @param {Client} client The client instance. + * @param {String} webhookId The id of the webhook to modify. + * @param {Object} options The options to modify the webhook with. + * @param {String} options.channelId The id of the channel the webhook belongs to. + * @returns {Promise} + * @public + * @method + * @async + * @throws {TypeError} + * @static + */ + static modifyWebhook(client, webhookId, { channelId } = {}) { + if (!(client instanceof Client)) + throw new TypeError("GLUON: Client must be a Client instance."); + if (typeof webhookId !== "string") + throw new TypeError("GLUON: Webhook ID is not a string."); + if (typeof channelId !== "string") + throw new TypeError("GLUON: Channel ID is not a string."); + + const body = {}; + + body.channel_id = channelId; + + return client.request.makeRequest("patchModifyWebhook", [webhookId], body); + } + + /** + * Fetches a webhook by the webhook's id. + * @param {Client} client The client instance. + * @param {String} webhookId The id of the webhook to fetch. + * @returns {Promise} + * @public + * @method + * @async + * @throws {TypeError} + * @static + */ + static fetchWebhook(client, webhookId) { + if (!(client instanceof Client)) + throw new TypeError("GLUON: Client must be a Client instance."); + if (typeof webhookId !== "string") + throw new TypeError("GLUON: Webhook ID is not a string."); + return client.request.makeRequest("getWebhook", [webhookId]); + } + + /** + * Posts a webhook with the provided webhook id and token. + * @param {Client} client The client instance. + * @param {Object} referenceData An object with the webhook id and token. + * @param {String?} content The message to send with the webhook. + * @param {Object?} options Embeds, components and files to attach to the webhook. + * @returns {Promise} + * @public + * @method + * @async + * @throws {TypeError} + * @static + */ + static async postWebhook( + client, + { id, token }, + content, + { embeds, components, files } = {}, + ) { + if (!(client instanceof Client)) + throw new TypeError("GLUON: Client must be a Client instance."); + if (typeof id !== "string") + throw new TypeError("GLUON: Webhook ID is not a string."); + if (typeof token !== "string") + throw new TypeError("GLUON: Webhook token is not a string."); + + Message.sendValidation(content, { embeds, components, files }); + + const body = {}; + + if (content) body.content = content; + + if (embeds) body.embeds = embeds; + if (components) body.components; + if (files) body.files = files; + + await client.request.makeRequest("postExecuteWebhook", [id, token], body); + } + /** * Returns the icon URL of the guild. * @param {String} id The id of the guild.