mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 11:28:15 +00:00
refactor(helpers): separate functions into files (#667)
* refactor(helpers): separate functions into files * idk * idk
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
import { RequestManager } from "../../rest/request_manager.ts";
|
||||
import { structures } from "../../structures/mod.ts";
|
||||
import { CreateGuildPayload, CreateServerOptions } from "../../types/mod.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
|
||||
/** Create a new guild. Returns a guild object on success. Fires a Guild Create Gateway event. This endpoint can be used only by bots in less than 10 guilds. */
|
||||
export async function createGuild(options: CreateServerOptions) {
|
||||
const guild = (await RequestManager.post(
|
||||
endpoints.GUILDS,
|
||||
options,
|
||||
)) as CreateGuildPayload;
|
||||
|
||||
return structures.createGuildStruct(guild, 0);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { RequestManager } from "../../rest/request_manager.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
|
||||
/** Delete a guild permanently. User must be owner. Returns 204 No Content on success. Fires a Guild Delete Gateway event.
|
||||
*/
|
||||
export async function deleteServer(guildID: string) {
|
||||
const result = await RequestManager.delete(endpoints.GUILDS_BASE(guildID));
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import { RequestManager } from "../../rest/request_manager.ts";
|
||||
import { GuildEditOptions } from "../../types/mod.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
import { requireBotGuildPermissions } from "../../util/permissions.ts";
|
||||
import { urlToBase64 } from "../../util/utils.ts";
|
||||
|
||||
/** Modify a guilds settings. Requires the MANAGE_GUILD permission. */
|
||||
export async function editGuild(guildID: string, options: GuildEditOptions) {
|
||||
await requireBotGuildPermissions(guildID, ["MANAGE_GUILD"]);
|
||||
|
||||
if (options.icon && !options.icon.startsWith("data:image/")) {
|
||||
options.icon = await urlToBase64(options.icon);
|
||||
}
|
||||
|
||||
if (options.banner && !options.banner.startsWith("data:image/")) {
|
||||
options.banner = await urlToBase64(options.banner);
|
||||
}
|
||||
|
||||
if (options.splash && !options.splash.startsWith("data:image/")) {
|
||||
options.splash = await urlToBase64(options.splash);
|
||||
}
|
||||
|
||||
const result = await RequestManager.patch(
|
||||
endpoints.GUILDS_BASE(guildID),
|
||||
options,
|
||||
);
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { RequestManager } from "../../rest/request_manager.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
import { requireBotGuildPermissions } from "../../util/permissions.ts";
|
||||
|
||||
/** Modify a guild widget object for the guild. Requires the MANAGE_GUILD permission. */
|
||||
export async function editWidget(
|
||||
guildID: string,
|
||||
enabled: boolean,
|
||||
channelID?: string | null,
|
||||
) {
|
||||
await requireBotGuildPermissions(guildID, ["MANAGE_GUILD"]);
|
||||
|
||||
const result = await RequestManager.patch(endpoints.GUILD_WIDGET(guildID), {
|
||||
enabled,
|
||||
channel_id: channelID,
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import { RequestManager } from "../../rest/request_manager.ts";
|
||||
import { AuditLogs, GetAuditLogsOptions } from "../../types/mod.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
import { requireBotGuildPermissions } from "../../util/permissions.ts";
|
||||
|
||||
/** Returns the audit logs for the guild. Requires VIEW AUDIT LOGS permission */
|
||||
export async function getAuditLogs(
|
||||
guildID: string,
|
||||
options: GetAuditLogsOptions,
|
||||
) {
|
||||
await requireBotGuildPermissions(guildID, ["VIEW_AUDIT_LOG"]);
|
||||
|
||||
const result = await RequestManager.get(endpoints.GUILD_AUDIT_LOGS(guildID), {
|
||||
...options,
|
||||
action_type: options.action_type
|
||||
? AuditLogs[options.action_type]
|
||||
: undefined,
|
||||
limit: options.limit && options.limit >= 1 && options.limit <= 100
|
||||
? options.limit
|
||||
: 50,
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { RequestManager } from "../../rest/request_manager.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
|
||||
/** Returns an array of voice regions that can be used when creating servers. */
|
||||
export async function getAvailableVoiceRegions() {
|
||||
const result = await RequestManager.get(endpoints.VOICE_REGIONS);
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { RequestManager } from "../../rest/request_manager.ts";
|
||||
import { BannedUser } from "../../types/mod.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
import { requireBotGuildPermissions } from "../../util/permissions.ts";
|
||||
|
||||
/** 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 async function getBan(guildID: string, memberID: string) {
|
||||
await requireBotGuildPermissions(guildID, ["BAN_MEMBERS"]);
|
||||
|
||||
const result = await RequestManager.get(
|
||||
endpoints.GUILD_BAN(guildID, memberID),
|
||||
);
|
||||
|
||||
return result as BannedUser;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { RequestManager } from "../../rest/request_manager.ts";
|
||||
import { BannedUser } from "../../types/mod.ts";
|
||||
import { Collection } from "../../util/collection.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
import { requireBotGuildPermissions } from "../../util/permissions.ts";
|
||||
|
||||
/** Returns a list of ban objects for the users banned from this guild. Requires the BAN_MEMBERS permission. */
|
||||
export async function getBans(guildID: string) {
|
||||
await requireBotGuildPermissions(guildID, ["BAN_MEMBERS"]);
|
||||
|
||||
const results = (await RequestManager.get(
|
||||
endpoints.GUILD_BANS(guildID),
|
||||
)) as BannedUser[];
|
||||
|
||||
return new Collection<string, BannedUser>(
|
||||
results.map((res) => [res.user.id, res]),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { RequestManager } from "../../rest/request_manager.ts";
|
||||
import { UpdateGuildPayload } from "../../types/mod.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
|
||||
/**
|
||||
* ⚠️ **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 async function getGuild(guildID: string, counts = true) {
|
||||
const result = await RequestManager.get(endpoints.GUILDS_BASE(guildID), {
|
||||
with_counts: counts,
|
||||
});
|
||||
|
||||
return result as UpdateGuildPayload;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { RequestManager } from "../../rest/request_manager.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
|
||||
/** Returns the guild preview object for the given id. If the bot is not in the guild, then the guild must be Discoverable. */
|
||||
export async function getGuildPreview(guildID: string) {
|
||||
const result = await RequestManager.get(endpoints.GUILD_PREVIEW(guildID));
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { RequestManager } from "../../rest/request_manager.ts";
|
||||
import { Errors, PruneOptions, PrunePayload } from "../../types/mod.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
import { requireBotGuildPermissions } from "../../util/permissions.ts";
|
||||
import { camelKeysToSnakeCase, urlToBase64 } from "../../util/utils.ts";
|
||||
|
||||
/** Check how many members would be removed from the server in a prune operation. Requires the KICK_MEMBERS permission */
|
||||
export async function getPruneCount(guildID: string, options?: PruneOptions) {
|
||||
if (options?.days && options.days < 1) throw new Error(Errors.PRUNE_MIN_DAYS);
|
||||
if (options?.days && options.days > 30) {
|
||||
throw new Error(Errors.PRUNE_MAX_DAYS);
|
||||
}
|
||||
|
||||
await requireBotGuildPermissions(guildID, ["KICK_MEMBERS"]);
|
||||
|
||||
const result = await RequestManager.get(
|
||||
endpoints.GUILD_PRUNE(guildID),
|
||||
camelKeysToSnakeCase(options ?? {}),
|
||||
) as PrunePayload;
|
||||
|
||||
return result.pruned;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { RequestManager } from "../../rest/request_manager.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
|
||||
/** Returns the code and uses of the vanity url for this server if it is enabled. Requires the MANAGE_GUILD permission. */
|
||||
export async function getVanityURL(guildID: string) {
|
||||
const result = await RequestManager.get(endpoints.GUILD_VANITY_URL(guildID));
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { RequestManager } from "../../rest/request_manager.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
|
||||
/** Returns a list of voice region objects for the guild. Unlike the similar /voice route, this returns VIP servers when the guild is VIP-enabled. */
|
||||
export async function getVoiceRegions(guildID: string) {
|
||||
const result = await RequestManager.get(endpoints.GUILD_REGIONS(guildID));
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { cacheHandlers } from "../../cache.ts";
|
||||
import { RequestManager } from "../../rest/request_manager.ts";
|
||||
import { Errors } from "../../types/mod.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
|
||||
/** Returns the widget for the guild. */
|
||||
export async function getWidget(guildID: string, options?: { force: boolean }) {
|
||||
if (!options?.force) {
|
||||
const guild = await cacheHandlers.get("guilds", guildID);
|
||||
if (!guild) throw new Error(Errors.GUILD_NOT_FOUND);
|
||||
if (!guild?.widgetEnabled) throw new Error(Errors.GUILD_WIDGET_NOT_ENABLED);
|
||||
}
|
||||
|
||||
return RequestManager.get(`${endpoints.GUILD_WIDGET(guildID)}.json`);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { cacheHandlers } from "../../cache.ts";
|
||||
import { Errors } from "../../types/mod.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
|
||||
/** Returns the widget image URL for the guild. */
|
||||
export async function getWidgetImageURL(
|
||||
guildID: string,
|
||||
options?: {
|
||||
style?: "shield" | "banner1" | "banner2" | "banner3" | "banner4";
|
||||
force?: boolean;
|
||||
},
|
||||
) {
|
||||
if (!options?.force) {
|
||||
const guild = await cacheHandlers.get("guilds", guildID);
|
||||
if (!guild) throw new Error(Errors.GUILD_NOT_FOUND);
|
||||
if (!guild.widgetEnabled) throw new Error(Errors.GUILD_WIDGET_NOT_ENABLED);
|
||||
}
|
||||
|
||||
return `${endpoints.GUILD_WIDGET(guildID)}.png?style=${options?.style ??
|
||||
"shield"}`;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { RequestManager } from "../../rest/request_manager.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
import { requireBotGuildPermissions } from "../../util/permissions.ts";
|
||||
|
||||
/** Returns the guild widget object. Requires the MANAGE_GUILD permission. */
|
||||
export async function getWidgetSettings(guildID: string) {
|
||||
await requireBotGuildPermissions(guildID, ["MANAGE_GUILD"]);
|
||||
|
||||
const result = await RequestManager.get(endpoints.GUILD_WIDGET(guildID));
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { Guild } from "../../structures/mod.ts";
|
||||
import { ImageFormats, ImageSize } from "../../types/mod.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
import { formatImageURL } from "../../util/utils.ts";
|
||||
|
||||
/** The full URL of the banner from Discords CDN. Undefined if no banner is set. */
|
||||
export function guildBannerURL(
|
||||
guild: Guild,
|
||||
size: ImageSize = 128,
|
||||
format?: ImageFormats,
|
||||
) {
|
||||
return guild.banner
|
||||
? formatImageURL(
|
||||
endpoints.GUILD_BANNER(guild.id, guild.banner),
|
||||
size,
|
||||
format,
|
||||
)
|
||||
: undefined;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { Guild } from "../../structures/mod.ts";
|
||||
import { ImageFormats, ImageSize } from "../../types/mod.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
import { formatImageURL } from "../../util/utils.ts";
|
||||
|
||||
/** The full URL of the icon from Discords CDN. Undefined when no icon is set. */
|
||||
export function guildIconURL(
|
||||
guild: Guild,
|
||||
size: ImageSize = 128,
|
||||
format?: ImageFormats,
|
||||
) {
|
||||
return guild.icon
|
||||
? formatImageURL(endpoints.GUILD_ICON(guild.id, guild.icon), size, format)
|
||||
: undefined;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { Guild } from "../../structures/mod.ts";
|
||||
import { ImageFormats, ImageSize } from "../../types/mod.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
import { formatImageURL } from "../../util/utils.ts";
|
||||
|
||||
/** The full URL of the splash from Discords CDN. Undefined if no splash is set. */
|
||||
export function guildSplashURL(
|
||||
guild: Guild,
|
||||
size: ImageSize = 128,
|
||||
format?: ImageFormats,
|
||||
) {
|
||||
return guild.splash
|
||||
? formatImageURL(
|
||||
endpoints.GUILD_SPLASH(guild.id, guild.splash),
|
||||
size,
|
||||
format,
|
||||
)
|
||||
: undefined;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { RequestManager } from "../../rest/request_manager.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
|
||||
/** Leave a guild */
|
||||
export async function leaveGuild(guildID: string) {
|
||||
const result = await RequestManager.delete(endpoints.GUILD_LEAVE(guildID));
|
||||
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user