fir more things

This commit is contained in:
ITOH
2021-04-07 10:27:44 +02:00
parent 941ebd3beb
commit 313e7d2d35
16 changed files with 74 additions and 38 deletions

View File

@@ -1,27 +1,30 @@
import { rest } from "../../rest/rest.ts";
import { AuditLog } from "../../types/audit_log/audit_log.ts";
import { GetGuildAuditLog } from "../../types/audit_log/get_guild_audit_log.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import {
camelKeysToSnakeCase,
snakeKeysToCamelCase,
} from "../../util/utils.ts";
/** Returns the audit logs for the guild. Requires VIEW AUDIT LOGS permission */
export async function getAuditLogs(
guildId: string,
options: GetAuditLogsOptions,
options: GetGuildAuditLog,
) {
await requireBotGuildPermissions(guildId, ["VIEW_AUDIT_LOG"]);
const result = await rest.runMethod(
"get",
endpoints.GUILD_AUDIT_LOGS(guildId),
{
camelKeysToSnakeCase({
...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;
return snakeKeysToCamelCase(result) as AuditLog;
}

View File

@@ -1,9 +1,10 @@
import { rest } from "../../rest/rest.ts";
import { VoiceRegion } from "../../types/voice/voice_region.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 rest.runMethod("get", endpoints.VOICE_REGIONS);
return result;
return result as VoiceRegion;
}

View File

@@ -1,6 +1,8 @@
import { rest } from "../../rest/rest.ts";
import { Ban } from "../../types/guilds/ban.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import { snakeKeysToCamelCase } from "../../util/utils.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) {
@@ -11,5 +13,5 @@ export async function getBan(guildId: string, memberId: string) {
endpoints.GUILD_BAN(guildId, memberId),
);
return result as BannedUser;
return snakeKeysToCamelCase(result) as Ban;
}

View File

@@ -1,7 +1,9 @@
import { rest } from "../../rest/rest.ts";
import { Ban, DiscordBan } from "../../types/guilds/ban.ts";
import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import { snakeKeysToCamelCase } from "../../util/utils.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) {
@@ -10,9 +12,9 @@ export async function getBans(guildId: string) {
const results = (await rest.runMethod(
"get",
endpoints.GUILD_BANS(guildId),
)) as BannedUser[];
)) as DiscordBan[];
return new Collection<string, BannedUser>(
results.map((res) => [res.user.id, res]),
return new Collection<string, Ban>(
results.map((res) => [res.user.id, snakeKeysToCamelCase(res) as Ban]),
);
}

View File

@@ -1,5 +1,7 @@
import { rest } from "../../rest/rest.ts";
import { Guild } from "../../types/guilds/guild.ts";
import { endpoints } from "../../util/constants.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
/**
* ⚠️ **If you need this, you are probably doing something wrong. Always use cache.guilds.get()
@@ -13,5 +15,5 @@ export async function getGuild(guildId: string, counts = true) {
with_counts: counts,
});
return result as UpdateGuildPayload;
return snakeKeysToCamelCase(result) as Guild;
}

View File

@@ -1,9 +1,11 @@
import { rest } from "../../rest/rest.ts";
import { GuildPreview } from "../../types/guilds/guild_preview.ts";
import { endpoints } from "../../util/constants.ts";
import { snakeKeysToCamelCase } from "../../util/utils.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 rest.runMethod("get", endpoints.GUILD_PREVIEW(guildId));
return result;
return snakeKeysToCamelCase(result) as GuildPreview;
}

View File

@@ -1,11 +1,15 @@
import { rest } from "../../rest/rest.ts";
import { GetGuildPruneCountQuery } from "../../types/guilds/get_guild_prune_count.ts";
import { Errors } from "../../types/misc/errors.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import { camelKeysToSnakeCase } 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) {
export async function getPruneCount(
guildId: string,
options?: GetGuildPruneCountQuery,
) {
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);
@@ -17,7 +21,7 @@ export async function getPruneCount(guildId: string, options?: PruneOptions) {
"get",
endpoints.GUILD_PRUNE(guildId),
camelKeysToSnakeCase(options ?? {}),
) as PrunePayload;
);
return result.pruned;
return result.pruned as number;
}

View File

@@ -1,12 +1,16 @@
import { rest } from "../../rest/rest.ts";
import { InviteMetadata } from "../../types/invites/invite_metadata.ts";
import { endpoints } from "../../util/constants.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
/** Returns the code and uses of the vanity url for this server if it is enabled. Requires the MANAGE_GUILD permission. */
/** Returns the code and uses of the vanity url for this server if it is enabled else `code` will be null. Requires the `MANAGE_GUILD` permission. */
export async function getVanityURL(guildId: string) {
const result = await rest.runMethod(
"get",
endpoints.GUILD_VANITY_URL(guildId),
);
return result;
return snakeKeysToCamelCase(result) as
| (Partial<InviteMetadata> & Pick<InviteMetadata, "uses" | "code">)
| { code: null };
}

View File

@@ -1,9 +1,22 @@
import { rest } from "../../rest/rest.ts";
import {
DiscordVoiceRegion,
VoiceRegion,
} from "../../types/voice/voice_region.ts";
import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts";
import { snakeKeysToCamelCase } from "../../util/utils.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 rest.runMethod("get", endpoints.GUILD_REGIONS(guildId));
const result = await rest.runMethod(
"get",
endpoints.GUILD_REGIONS(guildId),
) as DiscordVoiceRegion[];
return result;
const convertedRegions = snakeKeysToCamelCase<VoiceRegion[]>(result);
return new Collection<string, VoiceRegion>(
convertedRegions.map((region) => [region.id, region]),
);
}

View File

@@ -1,14 +1,12 @@
import { cacheHandlers } from "../../cache.ts";
import { GetGuildWidgetImageQuery } from "../../types/guilds/get_guild_widget_image.ts";
import { Errors } from "../../types/misc/errors.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;
},
options?: GetGuildWidgetImageQuery & { force?: boolean },
) {
if (!options?.force) {
const guild = await cacheHandlers.get("guilds", guildId);

View File

@@ -6,7 +6,7 @@ import { formatImageURL } from "../../util/utils.ts";
/** The full URL of the banner from Discords CDN. Undefined if no banner is set. */
export function guildBannerURL(
id: string,
banner: string,
banner?: string,
size: DiscordImageSize = 128,
format?: DiscordImageFormat,
) {

View File

@@ -6,7 +6,7 @@ import { formatImageURL } from "../../util/utils.ts";
/** The full URL of the icon from Discords CDN. Undefined when no icon is set. */
export function guildIconURL(
id: string,
icon: string,
icon?: string,
size: DiscordImageSize = 128,
format?: DiscordImageFormat,
) {

View File

@@ -6,7 +6,7 @@ import { formatImageURL } from "../../util/utils.ts";
/** The full URL of the splash from Discords CDN. Undefined if no splash is set. */
export function guildSplashURL(
id: string,
splash: string,
splash?: string,
size: DiscordImageSize = 128,
format?: DiscordImageFormat,
) {

View File

@@ -2,7 +2,7 @@ import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
/** Leave a guild */
export async function leaveGuild(guildId: string) {
export async function leaveGuild(guildId: string): Promise<undefined> {
const result = await rest.runMethod("delete", endpoints.GUILD_LEAVE(guildId));
return result;

View File

@@ -1,6 +1,7 @@
import { SnakeCaseProps } from "../util.ts";
import { Invite } from "./invite.ts";
export interface InviteMetadata {
export interface InviteMetadata extends Invite {
/** Number of times this invite has been used */
uses: number;
/** Max number of times this invite can be used */

View File

@@ -72,7 +72,9 @@ function isObject(obj: unknown) {
}
// deno-lint-ignore no-explicit-any
export function camelKeysToSnakeCase(obj: Record<string, any>) {
export function camelKeysToSnakeCase<T>(
obj: Record<string, any> | Record<string, any>[],
): T {
if (isObject(obj)) {
// deno-lint-ignore no-explicit-any
const convertedObject: Record<string, any> = {};
@@ -80,20 +82,22 @@ export function camelKeysToSnakeCase(obj: Record<string, any>) {
Object.keys(obj)
.forEach((key) => {
convertedObject[camelToSnakeCase(key)] = camelKeysToSnakeCase(
obj[key],
(obj as Record<string, any>)[key],
);
});
return convertedObject;
return convertedObject as T;
} else if (Array.isArray(obj)) {
obj = obj.map((element) => camelKeysToSnakeCase(element));
}
return obj;
return obj as T;
}
// deno-lint-ignore no-explicit-any
export function snakeKeysToCamelCase(obj: Record<string, any>) {
export function snakeKeysToCamelCase<T>(
obj: Record<string, any> | Record<string, any>[],
): T {
if (isObject(obj)) {
// deno-lint-ignore no-explicit-any
const convertedObject: Record<string, any> = {};
@@ -101,16 +105,16 @@ export function snakeKeysToCamelCase(obj: Record<string, any>) {
Object.keys(obj)
.forEach((key) => {
convertedObject[snakeToCamelCase(key)] = snakeKeysToCamelCase(
obj[key],
(obj as Record<string, any>)[key],
);
});
return convertedObject;
return convertedObject as T;
} else if (Array.isArray(obj)) {
obj = obj.map((element) => snakeKeysToCamelCase(element));
}
return obj;
return obj as T;
}
/** @private */