Skip to content

Commit

Permalink
reorganise functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Starman3787 committed Aug 18, 2024
1 parent ab17af6 commit 5a86c74
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 187 deletions.
187 changes: 0 additions & 187 deletions src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>}
* @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<void>}
* @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<String>} messages An array of message ids to delete.
* @param {Object} options
* @returns {Promise<void>}
* @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<Object>}
* @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<Object>}
* @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<Object>}
* @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<Command>} commands Array of commands to register.
Expand Down Expand Up @@ -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<Array<Member>?>} 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.
Expand Down
42 changes: 42 additions & 0 deletions src/managers/ChannelMessageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>} messages An array of message ids to delete.
* @param {Object} options
* @returns {Promise<void>}
* @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;
45 changes: 45 additions & 0 deletions src/managers/GuildMemberManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Array<Member>?>} 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;
Loading

0 comments on commit 5a86c74

Please sign in to comment.