This commit is contained in:
TriForMine
2021-10-18 21:12:42 +02:00
parent b6d9eaf9aa
commit c0cf0ebba5
5 changed files with 29 additions and 41 deletions
+8 -11
View File
@@ -1,20 +1,17 @@
import { rest } from "../../rest/rest.ts";
import { CreateGuildEmoji } from "../../types/emojis/create_guild_emoji.ts";
import type { CreateGuildEmoji } from "../../types/emojis/create_guild_emoji.ts";
import type { Emoji } from "../../types/emojis/emoji.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import { urlToBase64 } from "../../util/utils.ts";
import type {Bot} from "../../bot.ts";
import type {SnakeCasedPropertiesDeep} from "../../types/util.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: bigint, name: string, image: string, options: CreateGuildEmoji) {
await requireBotGuildPermissions(guildId, ["MANAGE_EMOJIS"]);
export async function createEmoji(bot: Bot, guildId: bigint, name: string, image: string, options: CreateGuildEmoji) {
await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_EMOJIS"]);
if (image && !image.startsWith("data:image/")) {
image = await urlToBase64(image);
image = await bot.utils.urlToBase64(image);
}
const emoji = await rest.runMethod<Emoji>("post", endpoints.GUILD_EMOJIS(guildId), {
const emoji = await bot.rest.runMethod<SnakeCasedPropertiesDeep<Emoji>>("post", bot.constants.endpoints.GUILD_EMOJIS(guildId), {
...options,
name,
image,
@@ -22,6 +19,6 @@ export async function createEmoji(guildId: bigint, name: string, image: string,
return {
...emoji,
id: snowflakeToBigint(emoji.id!),
id: bot.transformers.snowflake(emoji.id!),
};
}
+4 -6
View File
@@ -1,10 +1,8 @@
import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import type {Bot} from "../../bot.ts";
/** Delete the given emoji. Requires the MANAGE_EMOJIS permission. Returns 204 No Content on success. */
export async function deleteEmoji(guildId: bigint, id: bigint, reason?: string) {
await requireBotGuildPermissions(guildId, ["MANAGE_EMOJIS"]);
export async function deleteEmoji(bot: Bot, guildId: bigint, id: bigint, reason?: string) {
await bot.utils.requireBotGuildPermissions(guildId, ["MANAGE_EMOJIS"]);
return await rest.runMethod<undefined>("delete", endpoints.GUILD_EMOJI(guildId, id), { reason });
return await bot.rest.runMethod<undefined>(bot.rest,"delete", bot.constants.endpoints.GUILD_EMOJI(guildId, id), { reason });
}
+5 -6
View File
@@ -1,12 +1,11 @@
import { rest } from "../../rest/rest.ts";
import type { Emoji } from "../../types/emojis/emoji.ts";
import type { ModifyGuildEmoji } from "../../types/emojis/modify_guild_emoji.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import type {Bot} from "../../bot.ts";
import type {SnakeCasedPropertiesDeep} from "../../types/util.ts";
/** Modify the given emoji. Requires the MANAGE_EMOJIS permission. */
export async function editEmoji(guildId: bigint, id: bigint, options: ModifyGuildEmoji) {
await requireBotGuildPermissions(guildId, ["MANAGE_EMOJIS"]);
export async function editEmoji(bot: Bot, guildId: bigint, id: bigint, options: ModifyGuildEmoji) {
await bot.utils.requireBotGuildPermissions(guildId, ["MANAGE_EMOJIS"]);
return await rest.runMethod<Emoji>("patch", endpoints.GUILD_EMOJI(guildId, id), options);
return await bot.rest.runMethod<SnakeCasedPropertiesDeep<Emoji>>("patch", bot.constants.endpoints.GUILD_EMOJI(guildId, id), options);
}
+5 -7
View File
@@ -1,22 +1,20 @@
import { cacheHandlers } from "../../cache.ts";
import { rest } from "../../rest/rest.ts";
import type { Emoji } from "../../types/emojis/emoji.ts";
import { Errors } from "../../types/discordeno/errors.ts";
import { endpoints } from "../../util/constants.ts";
import {Bot} from "../../bot.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: bigint, emojiId: bigint, addToCache = true) {
const result = await rest.runMethod<Emoji>("get", endpoints.GUILD_EMOJI(guildId, emojiId));
export async function getEmoji(bot: Bot, guildId: bigint, emojiId: bigint, addToCache = true) {
const result = await bot.rest.runMethod<Emoji>("get", bot.constants.endpoints.GUILD_EMOJI(guildId, emojiId));
if (addToCache) {
const guild = await cacheHandlers.get("guilds", guildId);
const guild = await bot.cache.guilds.get(guildId);
if (!guild) throw new Error(Errors.GUILD_NOT_FOUND);
guild.emojis.set(emojiId, result);
await cacheHandlers.set("guilds", guildId, guild);
await bot.cache.guilds.set(guildId, guild);
}
return result;
+7 -11
View File
@@ -1,30 +1,26 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { rest } from "../../rest/rest.ts";
import {Bot} from "../../bot.ts";
import type { Emoji } from "../../types/emojis/emoji.ts";
import { Errors } from "../../types/discordeno/errors.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
import { Collection } from "../../util/collection.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: bigint, addToCache = true) {
const result = await rest.runMethod<Emoji[]>("get", endpoints.GUILD_EMOJIS(guildId));
export async function getEmojis(bot: Bot, guildId: bigint, addToCache = true) {
const result = await bot.rest.runMethod<Emoji[]>(bot.rest,"get", bot.constants.endpoints.GUILD_EMOJIS(guildId));
if (addToCache) {
const guild = await cacheHandlers.get("guilds", guildId);
const guild = await bot.cache.guilds.get("guilds", guildId);
if (!guild) throw new Error(Errors.GUILD_NOT_FOUND);
result.forEach((emoji) => {
eventHandlers.debug?.("loop", `Running forEach loop in get_emojis file.`);
guild.emojis.set(snowflakeToBigint(emoji.id!), emoji);
bot.events.debug("loop", `Running forEach loop in get_emojis file.`);
guild.emojis.set(bot.transformers.snowflake(emoji.id!), emoji);
});
await cacheHandlers.set("guilds", guildId, guild);
await bot.cache.guilds.set(guildId, guild);
}
return new Collection(result.map((e) => [e.id!, e]));