refactor(helpers): separate functions into files (#667)

* refactor(helpers): separate functions into files

* idk

* idk
This commit is contained in:
ayntee
2021-03-13 08:10:31 -05:00
committed by GitHub
parent 88ce4da555
commit e9cbbbff7c
143 changed files with 3362 additions and 2915 deletions
+27
View File
@@ -0,0 +1,27 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { CreateEmojisOptions } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import { urlToBase64 } from "../../util/utils.ts";
/** Create an emoji in the server. Emojis and animated emojis have a maximum file size of 256kb. Attempting to upload an emoji larger than this limit will fail and return 400 Bad Request and an error message, but not a JSON status code. If a URL is provided to the image parameter, Discordeno will automatically convert it to a base64 string internally. */
export async function createEmoji(
guildID: string,
name: string,
image: string,
options: CreateEmojisOptions,
) {
await requireBotGuildPermissions(guildID, ["MANAGE_EMOJIS"]);
if (image && !image.startsWith("data:image/")) {
image = await urlToBase64(image);
}
const result = await RequestManager.post(endpoints.GUILD_EMOJIS(guildID), {
...options,
name,
image,
});
return result;
}
+19
View File
@@ -0,0 +1,19 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
/** Delete the given emoji. Requires the MANAGE_EMOJIS permission. Returns 204 No Content on success. */
export async function deleteEmoji(
guildID: string,
id: string,
reason?: string,
) {
await requireBotGuildPermissions(guildID, ["MANAGE_EMOJIS"]);
const result = await RequestManager.delete(
endpoints.GUILD_EMOJI(guildID, id),
{ reason },
);
return result;
}
+23
View File
@@ -0,0 +1,23 @@
import { RequestManager } from "../../rest/request_manager.ts";
import { EditEmojisOptions } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
/** Modify the given emoji. Requires the MANAGE_EMOJIS permission. */
export async function editEmoji(
guildID: string,
id: string,
options: EditEmojisOptions,
) {
await requireBotGuildPermissions(guildID, ["MANAGE_EMOJIS"]);
const result = await RequestManager.patch(
endpoints.GUILD_EMOJI(guildID, id),
{
name: options.name,
roles: options.roles,
},
);
return result;
}
+4
View File
@@ -0,0 +1,4 @@
/** Creates a url to the emoji from the Discord CDN. */
export function emojiURL(id: string, animated = false) {
return `https://cdn.discordapp.com/emojis/${id}.${animated ? "gif" : "png"}`;
}
+32
View File
@@ -0,0 +1,32 @@
import { cacheHandlers } from "../../cache.ts";
import { RequestManager } from "../../rest/request_manager.ts";
import { Emoji, Errors } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
/**
* Returns an emoji for the given guild and emoji ID.
*
* ⚠️ **If you need this, you are probably doing something wrong. Always use cache.guilds.get()?.emojis
*/
export async function getEmoji(
guildID: string,
emojiID: string,
addToCache = true,
) {
const result = (await RequestManager.get(
endpoints.GUILD_EMOJI(guildID, emojiID),
)) as Emoji;
if (addToCache) {
const guild = await cacheHandlers.get("guilds", guildID);
if (!guild) throw new Error(Errors.GUILD_NOT_FOUND);
guild.emojis.set(result.id ?? result.name, result);
cacheHandlers.set(
"guilds",
guildID,
guild,
);
}
return result;
}
+26
View File
@@ -0,0 +1,26 @@
import { cacheHandlers } from "../../cache.ts";
import { RequestManager } from "../../rest/request_manager.ts";
import { Emoji, Errors } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
/**
* Returns a list of emojis for the given guild.
*
* ⚠️ **If you need this, you are probably doing something wrong. Always use cache.guilds.get()?.emojis
*/
export async function getEmojis(guildID: string, addToCache = true) {
const result = (await RequestManager.get(
endpoints.GUILD_EMOJIS(guildID),
)) as Emoji[];
if (addToCache) {
const guild = await cacheHandlers.get("guilds", guildID);
if (!guild) throw new Error(Errors.GUILD_NOT_FOUND);
result.forEach((emoji) => guild.emojis.set(emoji.id ?? emoji.name, emoji));
cacheHandlers.set("guilds", guildID, guild);
}
return result;
}