mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 11:28:15 +00:00
Merge branch 'master' of https://github.com/Skillz4Killz/Discordeno into next
This commit is contained in:
+275
-95
@@ -1,4 +1,3 @@
|
||||
import { endpoints } from "../constants/discord.ts";
|
||||
import { cacheHandlers } from "../controllers/cache.ts";
|
||||
import { identifyPayload } from "../module/client.ts";
|
||||
import { RequestManager } from "../module/requestManager.ts";
|
||||
@@ -6,6 +5,7 @@ import { requestAllMembers } from "../module/shardingManager.ts";
|
||||
import { Guild } from "../structures/guild.ts";
|
||||
import { Member } from "../structures/member.ts";
|
||||
import { structures } from "../structures/mod.ts";
|
||||
import { Template } from "../structures/template.ts";
|
||||
import { ImageFormats, ImageSize } from "../types/cdn.ts";
|
||||
import { ChannelCreatePayload, ChannelTypes } from "../types/channel.ts";
|
||||
import { Errors } from "../types/errors.ts";
|
||||
@@ -14,16 +14,22 @@ import {
|
||||
BanOptions,
|
||||
ChannelCreateOptions,
|
||||
CreateEmojisOptions,
|
||||
CreateGuildFromTemplate,
|
||||
CreateGuildPayload,
|
||||
CreateGuildTemplate,
|
||||
CreateRoleOptions,
|
||||
CreateServerOptions,
|
||||
EditEmojisOptions,
|
||||
EditGuildTemplate,
|
||||
EditIntegrationOptions,
|
||||
FetchMembersOptions,
|
||||
GetAuditLogsOptions,
|
||||
GuildEditOptions,
|
||||
GuildTemplate,
|
||||
PositionSwap,
|
||||
PruneOptions,
|
||||
PrunePayload,
|
||||
UpdateGuildPayload,
|
||||
UserPayload,
|
||||
} from "../types/guild.ts";
|
||||
import { MemberCreatePayload } from "../types/member.ts";
|
||||
@@ -32,6 +38,7 @@ import { Permissions } from "../types/permission.ts";
|
||||
import { RoleData } from "../types/role.ts";
|
||||
import { formatImageURL } from "../utils/cdn.ts";
|
||||
import { Collection } from "../utils/collection.ts";
|
||||
import { endpoints } from "../utils/constants.ts";
|
||||
import { botHasPermission, calculateBits } from "../utils/permissions.ts";
|
||||
import { urlToBase64 } from "../utils/utils.ts";
|
||||
|
||||
@@ -97,7 +104,11 @@ export async function createGuildChannel(
|
||||
name: string,
|
||||
options?: ChannelCreateOptions,
|
||||
) {
|
||||
if (!botHasPermission(guild.id, [Permissions.MANAGE_CHANNELS])) {
|
||||
const hasPerm = await botHasPermission(
|
||||
guild.id,
|
||||
[Permissions.MANAGE_CHANNELS],
|
||||
);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_CHANNELS);
|
||||
}
|
||||
|
||||
@@ -126,12 +137,16 @@ export async function createGuildChannel(
|
||||
}
|
||||
|
||||
/** Delete a channel in your server. Bot needs MANAGE_CHANNEL permissions in the server. */
|
||||
export function deleteChannel(
|
||||
export async function deleteChannel(
|
||||
guildID: string,
|
||||
channelID: string,
|
||||
reason?: string,
|
||||
) {
|
||||
if (!botHasPermission(guildID, [Permissions.MANAGE_CHANNELS])) {
|
||||
const hasPerm = await botHasPermission(
|
||||
guildID,
|
||||
[Permissions.MANAGE_CHANNELS],
|
||||
);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_CHANNELS);
|
||||
}
|
||||
|
||||
@@ -186,16 +201,20 @@ export function swapChannels(
|
||||
*
|
||||
* ⚠️ **ADVANCED USE ONLY: Your members will be cached in your guild most likely. Only use this when you are absolutely sure the member is not cached.**
|
||||
*/
|
||||
export async function getMember(guildID: string, id: string) {
|
||||
export async function getMember(
|
||||
guildID: string,
|
||||
id: string,
|
||||
options?: { force?: boolean },
|
||||
) {
|
||||
const guild = await cacheHandlers.get("guilds", guildID);
|
||||
if (!guild) return;
|
||||
if (!guild && !options?.force) return;
|
||||
|
||||
const data = await RequestManager.get(
|
||||
endpoints.GUILD_MEMBER(guildID, id),
|
||||
) as MemberCreatePayload;
|
||||
|
||||
const member = await structures.createMember(data, guild.id);
|
||||
guild.members.set(id, member);
|
||||
const member = await structures.createMember(data, guildID);
|
||||
guild?.members.set(id, member);
|
||||
return member;
|
||||
}
|
||||
|
||||
@@ -223,9 +242,8 @@ export async function createEmoji(
|
||||
image: string,
|
||||
options: CreateEmojisOptions,
|
||||
) {
|
||||
if (
|
||||
!botHasPermission(guildID, [Permissions.MANAGE_EMOJIS])
|
||||
) {
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_EMOJIS]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_EMOJIS);
|
||||
}
|
||||
|
||||
@@ -241,16 +259,16 @@ export async function createEmoji(
|
||||
}
|
||||
|
||||
/** Modify the given emoji. Requires the MANAGE_EMOJIS permission. */
|
||||
export function editEmoji(
|
||||
export async function editEmoji(
|
||||
guildID: string,
|
||||
id: string,
|
||||
options: EditEmojisOptions,
|
||||
) {
|
||||
if (
|
||||
!botHasPermission(guildID, [Permissions.MANAGE_EMOJIS])
|
||||
) {
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_EMOJIS]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_EMOJIS);
|
||||
}
|
||||
|
||||
return RequestManager.patch(endpoints.GUILD_EMOJI(guildID, id), {
|
||||
name: options.name,
|
||||
roles: options.roles,
|
||||
@@ -258,12 +276,16 @@ export function editEmoji(
|
||||
}
|
||||
|
||||
/** Delete the given emoji. Requires the MANAGE_EMOJIS permission. Returns 204 No Content on success. */
|
||||
export function deleteEmoji(guildID: string, id: string, reason?: string) {
|
||||
if (
|
||||
!botHasPermission(guildID, [Permissions.MANAGE_EMOJIS])
|
||||
) {
|
||||
export async function deleteEmoji(
|
||||
guildID: string,
|
||||
id: string,
|
||||
reason?: string,
|
||||
) {
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_EMOJIS]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_EMOJIS);
|
||||
}
|
||||
|
||||
return RequestManager.delete(
|
||||
endpoints.GUILD_EMOJI(guildID, id),
|
||||
{ reason },
|
||||
@@ -281,11 +303,11 @@ export async function createGuildRole(
|
||||
options: CreateRoleOptions,
|
||||
reason?: string,
|
||||
) {
|
||||
if (
|
||||
!botHasPermission(guildID, [Permissions.MANAGE_ROLES])
|
||||
) {
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_ROLES]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_ROLES);
|
||||
}
|
||||
|
||||
const result = await RequestManager.post(
|
||||
endpoints.GUILD_ROLES(guildID),
|
||||
{
|
||||
@@ -307,16 +329,16 @@ export async function createGuildRole(
|
||||
}
|
||||
|
||||
/** Edit a guild role. Requires the MANAGE_ROLES permission. */
|
||||
export function editRole(
|
||||
export async function editRole(
|
||||
guildID: string,
|
||||
id: string,
|
||||
options: CreateRoleOptions,
|
||||
) {
|
||||
if (
|
||||
!botHasPermission(guildID, [Permissions.MANAGE_ROLES])
|
||||
) {
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_ROLES]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_ROLES);
|
||||
}
|
||||
|
||||
return RequestManager.patch(endpoints.GUILD_ROLE(guildID, id), {
|
||||
...options,
|
||||
permissions: options.permissions
|
||||
@@ -326,12 +348,12 @@ export function editRole(
|
||||
}
|
||||
|
||||
/** Delete a guild role. Requires the MANAGE_ROLES permission. */
|
||||
export function deleteRole(guildID: string, id: string) {
|
||||
if (
|
||||
!botHasPermission(guildID, [Permissions.MANAGE_ROLES])
|
||||
) {
|
||||
export async function deleteRole(guildID: string, id: string) {
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_ROLES]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_ROLES);
|
||||
}
|
||||
|
||||
return RequestManager.delete(endpoints.GUILD_ROLE(guildID, id));
|
||||
}
|
||||
|
||||
@@ -339,22 +361,22 @@ export function deleteRole(guildID: string, id: string) {
|
||||
*
|
||||
* ⚠️ **If you need this, you are probably doing something wrong. This is not intended for use. Your roles will be cached in your guild.**
|
||||
*/
|
||||
export function getRoles(guildID: string) {
|
||||
if (
|
||||
!botHasPermission(guildID, [Permissions.MANAGE_ROLES])
|
||||
) {
|
||||
export async function getRoles(guildID: string) {
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_ROLES]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_ROLES);
|
||||
}
|
||||
|
||||
return RequestManager.get(endpoints.GUILD_ROLES(guildID));
|
||||
}
|
||||
|
||||
/** Modify the positions of a set of role objects for the guild. Requires the MANAGE_ROLES permission. */
|
||||
export function swapRoles(guildID: string, rolePositons: PositionSwap) {
|
||||
if (
|
||||
!botHasPermission(guildID, [Permissions.MANAGE_ROLES])
|
||||
) {
|
||||
export async function swapRoles(guildID: string, rolePositons: PositionSwap) {
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_ROLES]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_ROLES);
|
||||
}
|
||||
|
||||
return RequestManager.patch(endpoints.GUILD_ROLES(guildID), rolePositons);
|
||||
}
|
||||
|
||||
@@ -363,9 +385,9 @@ export async function getPruneCount(guildID: string, options: PruneOptions) {
|
||||
if (options.days < 1) {
|
||||
throw new Error(Errors.PRUNE_MIN_DAYS);
|
||||
}
|
||||
if (
|
||||
!botHasPermission(guildID, [Permissions.KICK_MEMBERS])
|
||||
) {
|
||||
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.KICK_MEMBERS]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_KICK_MEMBERS);
|
||||
}
|
||||
|
||||
@@ -378,13 +400,13 @@ export async function getPruneCount(guildID: string, options: PruneOptions) {
|
||||
}
|
||||
|
||||
/** Begin pruning all members in the given time period */
|
||||
export function pruneMembers(guildID: string, options: PruneOptions) {
|
||||
export async function pruneMembers(guildID: string, options: PruneOptions) {
|
||||
if (options.days < 1) {
|
||||
throw new Error(Errors.PRUNE_MIN_DAYS);
|
||||
}
|
||||
if (
|
||||
!botHasPermission(guildID, [Permissions.KICK_MEMBERS])
|
||||
) {
|
||||
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.KICK_MEMBERS]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_KICK_MEMBERS);
|
||||
}
|
||||
|
||||
@@ -405,8 +427,12 @@ export function fetchMembers(guild: Guild, options?: FetchMembersOptions) {
|
||||
}
|
||||
|
||||
/** Returns the audit logs for the guild. Requires VIEW AUDIT LOGS permission */
|
||||
export function getAuditLogs(guildID: string, options: GetAuditLogsOptions) {
|
||||
if (!botHasPermission(guildID, [Permissions.VIEW_AUDIT_LOG])) {
|
||||
export async function getAuditLogs(
|
||||
guildID: string,
|
||||
options: GetAuditLogsOptions,
|
||||
) {
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.VIEW_AUDIT_LOG]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_VIEW_AUDIT_LOG);
|
||||
}
|
||||
|
||||
@@ -419,26 +445,26 @@ export function getAuditLogs(guildID: string, options: GetAuditLogsOptions) {
|
||||
}
|
||||
|
||||
/** Returns the guild embed object. Requires the MANAGE_GUILD permission. */
|
||||
export function getEmbed(guildID: string) {
|
||||
if (
|
||||
!botHasPermission(guildID, [Permissions.MANAGE_GUILD])
|
||||
) {
|
||||
export async function getEmbed(guildID: string) {
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_GUILD);
|
||||
}
|
||||
|
||||
return RequestManager.get(endpoints.GUILD_EMBED(guildID));
|
||||
}
|
||||
|
||||
/** Modify a guild embed object for the guild. Requires the MANAGE_GUILD permission. */
|
||||
export function editEmbed(
|
||||
export async function editEmbed(
|
||||
guildID: string,
|
||||
enabled: boolean,
|
||||
channelID?: string | null,
|
||||
) {
|
||||
if (
|
||||
!botHasPermission(guildID, [Permissions.MANAGE_GUILD])
|
||||
) {
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_GUILD);
|
||||
}
|
||||
|
||||
return RequestManager.patch(
|
||||
endpoints.GUILD_EMBED(guildID),
|
||||
{ enabled, channel_id: channelID },
|
||||
@@ -451,26 +477,26 @@ export function getVanityURL(guildID: string) {
|
||||
}
|
||||
|
||||
/** Returns a list of integrations for the guild. Requires the MANAGE_GUILD permission. */
|
||||
export function getIntegrations(guildID: string) {
|
||||
if (
|
||||
!botHasPermission(guildID, [Permissions.MANAGE_GUILD])
|
||||
) {
|
||||
export async function getIntegrations(guildID: string) {
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_GUILD);
|
||||
}
|
||||
|
||||
return RequestManager.get(endpoints.GUILD_INTEGRATIONS(guildID));
|
||||
}
|
||||
|
||||
/** Modify the behavior and settings of an integration object for the guild. Requires the MANAGE_GUILD permission. */
|
||||
export function editIntegration(
|
||||
export async function editIntegration(
|
||||
guildID: string,
|
||||
id: string,
|
||||
options: EditIntegrationOptions,
|
||||
) {
|
||||
if (
|
||||
!botHasPermission(guildID, [Permissions.MANAGE_GUILD])
|
||||
) {
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_GUILD);
|
||||
}
|
||||
|
||||
return RequestManager.patch(
|
||||
endpoints.GUILD_INTEGRATION(guildID, id),
|
||||
options,
|
||||
@@ -478,30 +504,29 @@ export function editIntegration(
|
||||
}
|
||||
|
||||
/** Delete the attached integration object for the guild with this id. Requires MANAGE_GUILD permission. */
|
||||
export function deleteIntegration(guildID: string, id: string) {
|
||||
if (
|
||||
!botHasPermission(guildID, [Permissions.MANAGE_GUILD])
|
||||
) {
|
||||
export async function deleteIntegration(guildID: string, id: string) {
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_GUILD);
|
||||
}
|
||||
|
||||
return RequestManager.delete(endpoints.GUILD_INTEGRATION(guildID, id));
|
||||
}
|
||||
|
||||
/** Sync an integration. Requires the MANAGE_GUILD permission. */
|
||||
export function syncIntegration(guildID: string, id: string) {
|
||||
if (
|
||||
!botHasPermission(guildID, [Permissions.MANAGE_GUILD])
|
||||
) {
|
||||
export async function syncIntegration(guildID: string, id: string) {
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_GUILD);
|
||||
}
|
||||
|
||||
return RequestManager.post(endpoints.GUILD_INTEGRATION_SYNC(guildID, id));
|
||||
}
|
||||
|
||||
/** Returns a list of ban objects for the users banned from this guild. Requires the BAN_MEMBERS permission. */
|
||||
export async function getBans(guildID: string) {
|
||||
if (
|
||||
!botHasPermission(guildID, [Permissions.BAN_MEMBERS])
|
||||
) {
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.BAN_MEMBERS]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_BAN_MEMBERS);
|
||||
}
|
||||
|
||||
@@ -515,10 +540,9 @@ export async function getBans(guildID: string) {
|
||||
}
|
||||
|
||||
/** Returns a ban object for the given user or a 404 not found if the ban cannot be found. Requires the BAN_MEMBERS permission. */
|
||||
export function getBan(guildID: string, memberID: string) {
|
||||
if (
|
||||
!botHasPermission(guildID, [Permissions.BAN_MEMBERS])
|
||||
) {
|
||||
export async function getBan(guildID: string, memberID: string) {
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.BAN_MEMBERS]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_BAN_MEMBERS);
|
||||
}
|
||||
|
||||
@@ -528,10 +552,9 @@ export function getBan(guildID: string, memberID: string) {
|
||||
}
|
||||
|
||||
/** Ban a user from the guild and optionally delete previous messages sent by the user. Requires the BAN_MEMBERS permission. */
|
||||
export function ban(guildID: string, id: string, options: BanOptions) {
|
||||
if (
|
||||
!botHasPermission(guildID, [Permissions.BAN_MEMBERS])
|
||||
) {
|
||||
export async function ban(guildID: string, id: string, options: BanOptions) {
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.BAN_MEMBERS]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_BAN_MEMBERS);
|
||||
}
|
||||
|
||||
@@ -542,10 +565,9 @@ export function ban(guildID: string, id: string, options: BanOptions) {
|
||||
}
|
||||
|
||||
/** Remove the ban for a user. REquires BAN_MEMBERS permission */
|
||||
export function unban(guildID: string, id: string) {
|
||||
if (
|
||||
!botHasPermission(guildID, [Permissions.BAN_MEMBERS])
|
||||
) {
|
||||
export async function unban(guildID: string, id: string) {
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.BAN_MEMBERS]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_BAN_MEMBERS);
|
||||
}
|
||||
return RequestManager.delete(endpoints.GUILD_BAN(guildID, id));
|
||||
@@ -553,9 +575,8 @@ export function unban(guildID: string, id: string) {
|
||||
|
||||
/** Modify a guilds settings. Requires the MANAGE_GUILD permission. */
|
||||
export async function editGuild(guildID: string, options: GuildEditOptions) {
|
||||
if (
|
||||
!botHasPermission(guildID, [Permissions.MANAGE_GUILD])
|
||||
) {
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_GUILD);
|
||||
}
|
||||
|
||||
@@ -575,12 +596,12 @@ export async function editGuild(guildID: string, options: GuildEditOptions) {
|
||||
}
|
||||
|
||||
/** Get all the invites for this guild. Requires MANAGE_GUILD permission */
|
||||
export function getInvites(guildID: string) {
|
||||
if (
|
||||
!botHasPermission(guildID, [Permissions.MANAGE_GUILD])
|
||||
) {
|
||||
export async function getInvites(guildID: string) {
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_GUILD);
|
||||
}
|
||||
|
||||
return RequestManager.get(endpoints.GUILD_INVITES(guildID));
|
||||
}
|
||||
|
||||
@@ -595,8 +616,12 @@ export function getVoiceRegions(guildID: string) {
|
||||
}
|
||||
|
||||
/** Returns a list of guild webhooks objects. Requires the MANAGE_WEBHOOKs permission. */
|
||||
export function getWebhooks(guildID: string) {
|
||||
if (!botHasPermission(guildID, [Permissions.MANAGE_WEBHOOKS])) {
|
||||
export async function getWebhooks(guildID: string) {
|
||||
const hasPerm = await botHasPermission(
|
||||
guildID,
|
||||
[Permissions.MANAGE_WEBHOOKS],
|
||||
);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_WEBHOOKS);
|
||||
}
|
||||
|
||||
@@ -607,3 +632,158 @@ export function getWebhooks(guildID: string) {
|
||||
export function getUser(userID: string) {
|
||||
return RequestManager.get(endpoints.USER(userID)) as Promise<UserPayload>;
|
||||
}
|
||||
|
||||
/**
|
||||
* ⚠️ **If you need this, you are probably doing something wrong. Always use cache.guilds.get()
|
||||
*
|
||||
* Advanced Devs:
|
||||
* This function fetches a guild's data. This is not the same data as a GUILD_CREATE.
|
||||
* So it does not cache the guild, you must do it manually.
|
||||
* */
|
||||
export function getGuild(guildID: string, counts = true) {
|
||||
return RequestManager.get(
|
||||
endpoints.GUILD(guildID),
|
||||
{ with_counts: counts },
|
||||
) as Promise<UpdateGuildPayload>;
|
||||
}
|
||||
|
||||
/** Returns the guild template if it exists */
|
||||
export function getGuildTemplate(
|
||||
guildID: string,
|
||||
templateCode: string,
|
||||
) {
|
||||
return RequestManager.get(
|
||||
`${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`,
|
||||
) as Promise<Template>;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
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);
|
||||
}
|
||||
|
||||
const guild = await RequestManager.post(
|
||||
endpoints.GUILD_TEMPLATE(templateCode),
|
||||
data,
|
||||
) as Promise<CreateGuildPayload>;
|
||||
return guild;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of templates.
|
||||
* Requires the `MANAGE_GUILD` permission.
|
||||
*/
|
||||
export async function getGuildTemplates(guildID: string) {
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]);
|
||||
if (!hasPerm) throw new Error(Errors.MISSING_MANAGE_GUILD);
|
||||
|
||||
const templates = await RequestManager.get(
|
||||
endpoints.GUILD_TEMPLATES(guildID),
|
||||
) as GuildTemplate[];
|
||||
return templates.map((template) => structures.createTemplate(template));
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a template from a guild.
|
||||
* Requires the `MANAGE_GUILD` permission.
|
||||
*/
|
||||
export async function deleteGuildTemplate(
|
||||
guildID: string,
|
||||
templateCode: string,
|
||||
) {
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]);
|
||||
if (!hasPerm) throw new Error(Errors.MISSING_MANAGE_GUILD);
|
||||
|
||||
const deletedTemplate = await RequestManager.delete(
|
||||
`${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`,
|
||||
) as GuildTemplate;
|
||||
return structures.createTemplate(deletedTemplate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
export async function createGuildTemplate(
|
||||
guildID: string,
|
||||
data: CreateGuildTemplate,
|
||||
) {
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]);
|
||||
if (!hasPerm) throw new Error(Errors.MISSING_MANAGE_GUILD);
|
||||
|
||||
if (data.name.length < 1 || data.name.length > 100) {
|
||||
throw new Error("The name can only be in between 1-100 characters.");
|
||||
}
|
||||
|
||||
if (
|
||||
data.description?.length &&
|
||||
data.description.length > 120
|
||||
) {
|
||||
throw new Error("The description can only be in between 0-120 characters.");
|
||||
}
|
||||
|
||||
const template = await RequestManager.post(
|
||||
endpoints.GUILD_TEMPLATES(guildID),
|
||||
data,
|
||||
) as GuildTemplate;
|
||||
return structures.createTemplate(template);
|
||||
}
|
||||
|
||||
/**
|
||||
* Syncs the template to the guild's current state.
|
||||
* Requires the `MANAGE_GUILD` permission.
|
||||
*/
|
||||
export async function syncGuildTemplate(guildID: string, templateCode: string) {
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]);
|
||||
if (!hasPerm) throw new Error(Errors.MISSING_MANAGE_GUILD);
|
||||
|
||||
const template = await RequestManager.put(
|
||||
`${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`,
|
||||
) as GuildTemplate;
|
||||
return structures.createTemplate(template);
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit a template's metadata.
|
||||
* Requires the `MANAGE_GUILD` permission.
|
||||
*/
|
||||
export async function editGuildTemplate(
|
||||
guildID: string,
|
||||
templateCode: string,
|
||||
data: EditGuildTemplate,
|
||||
) {
|
||||
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]);
|
||||
if (!hasPerm) throw new Error(Errors.MISSING_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.");
|
||||
}
|
||||
|
||||
if (
|
||||
data.description?.length &&
|
||||
data.description.length > 120
|
||||
) {
|
||||
throw new Error("The description can only be in between 0-120 characters.");
|
||||
}
|
||||
|
||||
const template = await RequestManager.patch(
|
||||
`${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`,
|
||||
data,
|
||||
) as GuildTemplate;
|
||||
return structures.createTemplate(template);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user