Templates + Fixes

This commit is contained in:
TriForMine
2021-10-18 21:48:25 +02:00
parent b30b91a573
commit e6da05837b
18 changed files with 61 additions and 60 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
import type { Integration } from "../../types/integrations/integration.ts";
import {Bot} from "../../bot.ts";
import type {Bot} from "../../bot.ts";
import {SnakeCasedPropertiesDeep} from "../../types/util.ts";
/** Returns a list of integrations for the guild. Requires the MANAGE_GUILD permission. */
+1 -1
View File
@@ -1,5 +1,5 @@
import type { Message } from "../../types/messages/message.ts";
import {Bot} from "../../bot.ts";
import type {Bot} from "../../bot.ts";
import {SnakeCasedPropertiesDeep} from "../../types/util.ts";
/** Crosspost a message in a News Channel to following channels. */
+1 -1
View File
@@ -1,4 +1,4 @@
import {Bot} from "../../bot.ts";
import type {Bot} from "../../bot.ts";
/** Removes all reactions for all emojis on this message. */
export async function removeAllReactions(bot: Bot, channelId: bigint, messageId: bigint) {
+1 -1
View File
@@ -1,4 +1,4 @@
import {Bot} from "../../bot.ts";
import type {Bot} from "../../bot.ts";
/** Removes a reaction from the given user on this message, defaults to bot. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. */
export async function removeReaction(
@@ -1,5 +1,5 @@
/** Removes all reactions for a single emoji on this message. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. */
import {Bot} from "../../bot.ts";
import type {Bot} from "../../bot.ts";
export async function removeReactionEmoji(bot: Bot, channelId: bigint, messageId: bigint, reaction: string) {
+1 -1
View File
@@ -1,5 +1,5 @@
/** Unpin a message in a channel. Requires MANAGE_MESSAGES. */
import {Bot} from "../../bot.ts";
import type {Bot} from "../../bot.ts";
export async function unpin(bot: Bot, channelId: bigint, messageId: bigint): Promise<undefined> {
await bot.utils.requireBotChannelPermissions(bot, channelId, ["MANAGE_MESSAGES"]);
+1 -1
View File
@@ -1,4 +1,4 @@
import {Bot} from "../../bot.ts";
import type {Bot} from "../../bot.ts";
/** Add a role to the member */
export async function addRole(bot: Bot, guildId: bigint, memberId: bigint, roleId: bigint, reason?: string) {
+1 -1
View File
@@ -1,4 +1,4 @@
import {Bot} from "../../bot.ts";
import type {Bot} from "../../bot.ts";
/** Delete a guild role. Requires the MANAGE_ROLES permission. */
export async function deleteRole(bot: Bot, guildId: bigint, id: bigint) {
+1 -1
View File
@@ -1,6 +1,6 @@
import type { CreateGuildRole } from "../../types/guilds/create_guild_role.ts";
import type { Role } from "../../types/permissions/role.ts";
import {Bot} from "../../bot.ts";
import type {Bot} from "../../bot.ts";
import {SnakeCasedPropertiesDeep} from "../../types/util.ts";
/** Edit a guild role. Requires the MANAGE_ROLES permission. */
+1 -1
View File
@@ -1,5 +1,5 @@
import type { Role } from "../../types/permissions/role.ts";
import {Bot} from "../../bot.ts";
import type {Bot} from "../../bot.ts";
import {Collection} from "../../util/collection.ts";
import {DiscordenoRole} from "../../transformers/role.ts";
/** Returns a list of role objects for the guild.
+1 -1
View File
@@ -1,4 +1,4 @@
import {Bot} from "../../bot.ts";
import type {Bot} from "../../bot.ts";
/** Remove a role from the member */
export async function removeRole(bot: Bot, guildId: bigint, memberId: bigint, roleId: bigint, reason?: string) {
@@ -1,29 +1,24 @@
import { cacheHandlers } from "../../cache.ts";
import { rest } from "../../rest/rest.ts";
import { structures } from "../../structures/mod.ts";
import type { Guild } from "../../types/guilds/guild.ts";
import type { CreateGuildFromTemplate } from "../../types/templates/create_guild_from_template.ts";
import { endpoints } from "../../util/constants.ts";
import { urlToBase64 } from "../../util/utils.ts";
import { ws } from "../../ws/ws.ts";
import type {Bot} from "../../bot.ts";
/**
* Create a new guild based on a template
* NOTE: This endpoint can be used only by bots in less than 10 guilds.
*/
export async function createGuildFromTemplate(templateCode: string, data: CreateGuildFromTemplate) {
if ((await cacheHandlers.size("guilds")) >= 10) {
export async function createGuildFromTemplate(bot: Bot, templateCode: string, data: CreateGuildFromTemplate) {
if ((await bot.cache.guilds.size()) >= 10) {
throw new Error("This function can only be used by bots in less than 10 guilds.");
}
if (data.icon) {
data.icon = await urlToBase64(data.icon);
data.icon = await bot.utils.urlToBase64(data.icon);
}
const createdGuild = await rest.runMethod<Guild>("post", endpoints.GUILD_TEMPLATE(templateCode), data);
const createdGuild = await bot.rest.runMethod<Guild>(bot.rest,"post", bot.constants.endpoints.GUILD_TEMPLATE(templateCode), data);
return await structures.createDiscordenoGuild(
return bot.transformers.guild(
createdGuild,
Number((BigInt(createdGuild.id) >> 22n % BigInt(ws.botGatewayData.shards)).toString())
Number((BigInt(createdGuild.id) >> 22n % BigInt(bot.ws.botGatewayData.shards)).toString())
);
}
+21 -9
View File
@@ -1,17 +1,17 @@
import { rest } from "../../rest/rest.ts";
import type { Template } from "../../types/templates/template.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import { snakelize } from "../../util/utils.ts";
import type {Bot} from "../../bot.ts";
import {User} from "../../types/users/user.ts";
import {Guild} from "../../types/guilds/guild.ts";
/**
* Creates a template for the guild.
* Requires the `MANAGE_GUILD` permission.
* @param name name of the template (1-100 characters)
* @param description description for the template (0-120 characters
* @param bot
* @param guildId
* @param data
*/
export async function createGuildTemplate(guildId: bigint, data: Template) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
export async function createGuildTemplate(bot: Bot, guildId: bigint, data: Template) {
await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_GUILD"]);
if (data.name.length < 1 || data.name.length > 100) {
throw new Error("The name can only be in between 1-100 characters.");
@@ -21,5 +21,17 @@ export async function createGuildTemplate(guildId: bigint, data: Template) {
throw new Error("The description can only be in between 0-120 characters.");
}
return await rest.runMethod<Template>("post", endpoints.GUILD_TEMPLATES(guildId), snakelize(data));
return await bot.rest.runMethod<Template>(bot.rest,"post", bot.constants.endpoints.GUILD_TEMPLATES(guildId), {
code: data.code,
name: data.name,
description: data.description,
usage_count: data.usageCount,
creator_id: data.creatorId,
creator: data.creator,
created_at: data.createdAt,
updated_at: data.updatedAt,
source_guild_id: data.sourceGuildId,
serialized_source_guild: data.serializedSourceGuild,
is_dirty: data.isDirty
});
}
@@ -1,14 +1,12 @@
import { rest } from "../../rest/rest.ts";
import type { Template } from "../../types/templates/template.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import type {Bot} from "../../bot.ts";
/**
* Deletes a template from a guild.
* Requires the `MANAGE_GUILD` permission.
*/
export async function deleteGuildTemplate(guildId: bigint, templateCode: string) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
export async function deleteGuildTemplate(bot: Bot, guildId: bigint, templateCode: string) {
await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_GUILD"]);
return await rest.runMethod<Template>("delete", `${endpoints.GUILD_TEMPLATES(guildId)}/${templateCode}`);
return await bot.rest.runMethod<Template>(bot.rest,"delete", `${bot.constants.endpoints.GUILD_TEMPLATES(guildId)}/${templateCode}`);
}
+7 -6
View File
@@ -1,15 +1,13 @@
import { rest } from "../../rest/rest.ts";
import type { ModifyGuildTemplate } from "../../types/templates/modify_guild_template.ts";
import type { Template } from "../../types/templates/template.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import type {Bot} from "../../bot.ts";
/**
* Edit a template's metadata.
* Requires the `MANAGE_GUILD` permission.
*/
export async function editGuildTemplate(guildId: bigint, templateCode: string, data: ModifyGuildTemplate) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
export async function editGuildTemplate(bot: Bot, guildId: bigint, templateCode: string, data: ModifyGuildTemplate) {
await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_GUILD"]);
if (data.name?.length && (data.name.length < 1 || data.name.length > 100)) {
throw new Error("The name can only be in between 1-100 characters.");
@@ -19,5 +17,8 @@ export async function editGuildTemplate(guildId: bigint, templateCode: string, d
throw new Error("The description can only be in between 0-120 characters.");
}
return await rest.runMethod<Template>("patch", `${endpoints.GUILD_TEMPLATES(guildId)}/${templateCode}`, data);
return await bot.rest.runMethod<Template>(bot.rest,"patch", `${bot.constants.endpoints.GUILD_TEMPLATES(guildId)}/${templateCode}`, {
name: data.name,
description: data.description
});
}
+4 -6
View File
@@ -1,17 +1,15 @@
import { rest } from "../../rest/rest.ts";
import type { Template } from "../../types/templates/template.ts";
import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import type {Bot} from "../../bot.ts";
/**
* Returns an array of templates.
* Requires the `MANAGE_GUILD` permission.
*/
export async function getGuildTemplates(guildId: bigint) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
export async function getGuildTemplates(bot: Bot, guildId: bigint) {
await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_GUILD"]);
const templates = await rest.runMethod<Template[]>("get", endpoints.GUILD_TEMPLATES(guildId));
const templates = await bot.rest.runMethod<Template[]>("get", bot.constants.endpoints.GUILD_TEMPLATES(guildId));
return new Collection(templates.map((template) => [template.code, template]));
}
+3 -4
View File
@@ -1,8 +1,7 @@
import { rest } from "../../rest/rest.ts";
import type { Template } from "../../types/templates/template.ts";
import { endpoints } from "../../util/constants.ts";
import type {Bot} from "../../bot.ts";
/** Returns the guild template if it exists */
export async function getTemplate(templateCode: string) {
return await rest.runMethod<Template>("get", endpoints.GUILD_TEMPLATE(templateCode));
export async function getTemplate(bot: Bot, templateCode: string) {
return await bot.rest.runMethod<Template>(bot.rest, "get", bot.constants.endpoints.GUILD_TEMPLATE(templateCode));
}
+4 -6
View File
@@ -1,14 +1,12 @@
import { rest } from "../../rest/rest.ts";
import type { Template } from "../../types/templates/template.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import type {Bot} from "../../bot.ts";
/**
* Syncs the template to the guild's current state.
* Requires the `MANAGE_GUILD` permission.
*/
export async function syncGuildTemplate(guildId: bigint, templateCode: string) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
export async function syncGuildTemplate(bot: Bot, guildId: bigint, templateCode: string) {
await bot.utils.requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
return await rest.runMethod<Template>("put", `${endpoints.GUILD_TEMPLATES(guildId)}/${templateCode}`);
return await bot. rest.runMethod<Template>(bot.rest,"put", `${bot.constants.endpoints.GUILD_TEMPLATES(guildId)}/${templateCode}`);
}