Skip to content

Commit

Permalink
move fetch message function
Browse files Browse the repository at this point in the history
  • Loading branch information
Starman3787 committed Aug 17, 2024
1 parent b210bb4 commit 57803e1
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 80 deletions.
30 changes: 0 additions & 30 deletions src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,36 +420,6 @@ class Client extends EventsEmitter {
return this.guilds.toJSON(TO_JSON_TYPES_ENUM.CACHE_FORMAT);
}

/**
* Fetches a message from a specific channel.
* @param {String} guild_id The ID of the guild that the message belongs to.
* @param {String} channel_id The ID of the channel that the message belongs to.
* @param {String} message_id The ID of the message to return.
* @returns {Promise<Message>}
* @public
* @method
* @async
* @throws {TypeError}
*/
async fetchMessage(guild_id, channel_id, message_id) {
if (typeof guild_id !== "string")
throw new TypeError("GLUON: Guild ID is not a string.");
if (typeof channel_id !== "string")
throw new TypeError("GLUON: Channel ID is not a string.");
if (typeof message_id !== "string")
throw new TypeError("GLUON: Message ID is not a string.");

const data = await this.request.makeRequest("getChannelMessage", [
channel_id,
message_id,
]);

return new Message(this, data, {
channel_id,
guild_id,
});
}

/**
* Posts a webhook with the provided webhook id and token.
* @param {Object} referenceData An object with the webhook id and token.
Expand Down
2 changes: 2 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,8 @@ export const LIMITS = {
MAX_NITRO_MESSAGE_CONTENT: 4000,
MAX_MESSAGE_EMBEDS: 10,
MAX_MESSAGE_FILES: 10,
MIN_MESSAGES_FETCH_LIMIT: 1,
MAX_MESSAGES_FETCH_LIMIT: 100,
};
export const COMMAND_NAME_REGEX =
/^[-_\p{L}\p{N}\p{sc=Deva}\p{sc=Thai}]{1,32}$/u;
Expand Down
259 changes: 209 additions & 50 deletions src/managers/ChannelMessageManager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Client from "../Client.js";
import { PERMISSIONS } from "../constants.js";
import Channel from "../structures/Channel.js";
import { LIMITS, PERMISSIONS } from "../constants.js";
import Message from "../structures/Message.js";
import checkPermission from "../util/discord/checkPermission.js";
import BaseCacheManager from "./BaseCacheManager.js";
Expand Down Expand Up @@ -42,6 +41,15 @@ class ChannelMessageManager extends BaseCacheManager {
this.#guild = guild;
}

/**
* The guild that this message manager belongs to.
* @type {Guild}
* @readonly
*/
get guild() {
return this.#guild;
}

/**
* Fetches a collection of messages or a singular message from the channel.
* @param {Object | String} options Either an object of {@link https://discord.com/developers/docs/resources/channel#get-channel-messages-query-string-params|options} or a message id.
Expand All @@ -67,57 +75,19 @@ class ChannelMessageManager extends BaseCacheManager {
)
throw new Error("MISSING PERMISSIONS: READ_MESSAGE_HISTORY");
if (typeof options === "object") {
if (this.toJSON().length != 0) {
const cachedMessages = [];
const rawCache = this.toJSON();
for (let i = 0; i < rawCache.length; i++) {
if (options.before && BigInt(rawCache[i].id) < BigInt(options.before))
cachedMessages.push(rawCache[i]);
else if (
options.after &&
BigInt(rawCache[i].id) > BigInt(options.after)
)
cachedMessages.push(rawCache[i]);
}
}

const body = {};

if (options.around) body.around = options.around;

if (options.before) body.before = options.before;

if (options.after) body.after = options.after;

if (options.limit) body.limit = options.limit;

const data = await this.#_client.request.makeRequest(
"getChannelMessages",
[this.#channel.id],
body,
ChannelMessageManager.fetchMessages(
this.#_client,
this.#guild.id,
this.#channel.id,
options,
);
const messages = [];
for (let i = 0; i < data.length; i++)
messages.push(
new Message(this.#_client, data[i], {
channel_id: data[i].channel_id,
guild_id: this.#channel.guild.id,
}),
);
return messages;
} else if (typeof options === "string") {
const cachedMessage = await this.get(options);
if (cachedMessage) return cachedMessage;

const data = await this.#_client.request.makeRequest(
"getChannelMessage",
[this.#channel.id, options],
return ChannelMessageManager.fetchMessage(
this.#_client,
this.#guild.id,
this.#channel.id,
options,
);

return new Message(this.#_client, data, {
channel_id: this.#channel.id,
guild_id: this.#channel.guild.id,
});
} else
throw new TypeError(
"GLUON: Must provide an object of options or a string of a message ID.",
Expand Down Expand Up @@ -170,6 +140,10 @@ class ChannelMessageManager extends BaseCacheManager {
* @param {String} guildId The ID of the guild.
* @param {String} channelId The ID of the channel.
* @returns {ChannelMessageManager}
* @public
* @static
* @method
* @throws {TypeError}
*/
static getCacheManager(client, guildId, channelId) {
if (!(client instanceof Client))
Expand All @@ -180,6 +154,191 @@ class ChannelMessageManager extends BaseCacheManager {
throw new TypeError("GLUON: Channel ID must be a string.");
return client.guilds.get(guildId).channels.get(channelId).messages;
}

/**
* Gets a message from the cache.
* @param {Client} client The client instance.
* @param {String} guildId The ID of the guild.
* @param {String} channelId The ID of the channel.
* @param {String} messageId The ID of the message.
* @returns {Message}
* @public
* @static
* @method
* @throws {TypeError}
*/
static getMessage(client, guildId, channelId, messageId) {
if (!(client instanceof Client))
throw new TypeError("GLUON: Client must be a Client instance.");
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.");
if (typeof messageId !== "string")
throw new TypeError("GLUON: Message ID must be a string.");
return ChannelMessageManager.getCacheManager(
client,
guildId,
channelId,
).get(messageId);
}

/**
* Fetches a message from the channel.
* @param {Client} client The client instance.
* @param {String} guildId The ID of the guild.
* @param {String} channelId The ID of the channel.
* @param {String} messageId The ID of the message.
* @returns {Message}
* @public
* @async
* @static
* @method
* @throws {TypeError | Error}
*/
static async fetchMessage(client, guildId, channelId, messageId) {
if (!(client instanceof Client))
throw new TypeError("GLUON: Client is not a Client instance.");
if (typeof guildId !== "string")
throw new TypeError("GLUON: Guild ID is not a string.");
if (typeof channelId !== "string")
throw new TypeError("GLUON: Channel ID is not a string.");
if (typeof messageId !== "string")
throw new TypeError("GLUON: Message ID is not a string.");

if (
!checkPermission(
(await GuildManager.getGuild(client, guildId).me()).permissions,
PERMISSIONS.VIEW_CHANNEL,
)
)
throw new Error("MISSING PERMISSIONS: VIEW_CHANNEL");
if (
!checkPermission(
(await GuildManager.getGuild(client, guildId).me()).permissions,
PERMISSIONS.READ_MESSAGE_HISTORY,
)
)
throw new Error("MISSING PERMISSIONS: READ_MESSAGE_HISTORY");

const fromCache = ChannelMessageManager.getMessage(
client,
guildId,
channelId,
messageId,
);
if (fromCache) return fromCache;

const data = await client.request.makeRequest("getChannelMessage", [
channelId,
messageId,
]);

return new Message(client, data, {
channelId,
guild_id: guildId,
});
}

/**
* Fetches a collection of messages from the channel.
* @param {Client} client The client instance.
* @param {String} guildId The ID of the guild.
* @param {String} channelId The ID of the channel.
* @param {Object} options The options for fetching messages.
* @param {String} options.around The ID of the message to fetch messages around.
* @param {String} options.before The ID of the message to fetch messages before.
* @param {String} options.after The ID of the message to fetch messages after.
* @param {Number} options.limit The maximum number of messages to fetch.
* @returns {Array<Message>}
* @public
* @async
* @static
* @method
* @throws {TypeError | Error}
*/
static async fetchMessages(
client,
guildId,
channelId,
{ around, before, after, limit },
) {
if (!(client instanceof Client))
throw new TypeError("GLUON: Client is not a Client instance.");
if (typeof guildId !== "string")
throw new TypeError("GLUON: Guild ID is not a string.");
if (typeof channelId !== "string")
throw new TypeError("GLUON: Channel ID is not a string.");
if (around && typeof around !== "string")
throw new TypeError("GLUON: Around is not a string.");
if (before && typeof before !== "string")
throw new TypeError("GLUON: Before is not a string.");
if (after && typeof after !== "string")
throw new TypeError("GLUON: After is not a string.");
if (typeof limit !== "undefined" && typeof limit !== "number")
throw new TypeError("GLUON: Limit is not a number.");
if (
typeof limit !== "undefined" &&
(limit < LIMITS.MIN_MESSAGES_FETCH_LIMIT ||
limit > LIMITS.MAX_MESSAGES_FETCH_LIMIT)
)
throw new RangeError(
`GLUON: Limit must be between ${LIMITS.MIN_MESSAGES_FETCH_LIMIT} and ${LIMITS.MAX_MESSAGES_FETCH_LIMIT}.`,
);

if (
!checkPermission(
(
await ChannelMessageManager.getCacheManager(
client,
guildId,
channelId,
).guild.me()
).permissions,
PERMISSIONS.VIEW_CHANNEL,
)
)
throw new Error("MISSING PERMISSIONS: VIEW_CHANNEL");
if (
!checkPermission(
(
await ChannelMessageManager.getCacheManager(
client,
guildId,
channelId,
).guild.me()
).permissions,
PERMISSIONS.READ_MESSAGE_HISTORY,
)
)
throw new Error("MISSING PERMISSIONS: READ_MESSAGE_HISTORY");

const body = {};

if (around) body.around = around;

if (before) body.before = before;

if (after) body.after = after;

if (limit) body.limit = limit;

const data = await client.request.makeRequest(
"getChannelMessages",
[channelId],
body,
);

const messages = [];
for (let i = 0; i < data.length; i++)
messages.push(
new Message(client, data[i], {
channel_id: data[i].channel_id,
guild_id: guildId,
}),
);
return messages;
}
}

export default ChannelMessageManager;

0 comments on commit 57803e1

Please sign in to comment.