mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 11:28:15 +00:00
guilds
This commit is contained in:
@@ -2,7 +2,6 @@ import { rest } from "../../rest/rest.ts";
|
|||||||
import { GuildWidget } from "../../types/guilds/guild_widget.ts";
|
import { GuildWidget } from "../../types/guilds/guild_widget.ts";
|
||||||
import { endpoints } from "../../util/constants.ts";
|
import { endpoints } from "../../util/constants.ts";
|
||||||
import { requireBotGuildPermissions } from "../../util/permissions.ts";
|
import { requireBotGuildPermissions } from "../../util/permissions.ts";
|
||||||
import { snakeKeysToCamelCase } from "../../util/utils.ts";
|
|
||||||
|
|
||||||
/** Modify a guild widget object for the guild. Requires the MANAGE_GUILD permission. */
|
/** Modify a guild widget object for the guild. Requires the MANAGE_GUILD permission. */
|
||||||
export async function editWidget(
|
export async function editWidget(
|
||||||
@@ -12,7 +11,7 @@ export async function editWidget(
|
|||||||
) {
|
) {
|
||||||
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
|
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
|
||||||
|
|
||||||
const result = await rest.runMethod(
|
return await rest.runMethod<GuildWidget>(
|
||||||
"patch",
|
"patch",
|
||||||
endpoints.GUILD_WIDGET(guildId),
|
endpoints.GUILD_WIDGET(guildId),
|
||||||
{
|
{
|
||||||
@@ -20,6 +19,4 @@ export async function editWidget(
|
|||||||
channel_id: channelId,
|
channel_id: channelId,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
return snakeKeysToCamelCase<GuildWidget>(result);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,10 +3,7 @@ import { AuditLog } from "../../types/audit_log/audit_log.ts";
|
|||||||
import { GetGuildAuditLog } from "../../types/audit_log/get_guild_audit_log.ts";
|
import { GetGuildAuditLog } from "../../types/audit_log/get_guild_audit_log.ts";
|
||||||
import { endpoints } from "../../util/constants.ts";
|
import { endpoints } from "../../util/constants.ts";
|
||||||
import { requireBotGuildPermissions } from "../../util/permissions.ts";
|
import { requireBotGuildPermissions } from "../../util/permissions.ts";
|
||||||
import {
|
import { camelKeysToSnakeCase } from "../../util/utils.ts";
|
||||||
camelKeysToSnakeCase,
|
|
||||||
snakeKeysToCamelCase,
|
|
||||||
} from "../../util/utils.ts";
|
|
||||||
|
|
||||||
/** Returns the audit logs for the guild. Requires VIEW AUDIT LOGS permission */
|
/** Returns the audit logs for the guild. Requires VIEW AUDIT LOGS permission */
|
||||||
export async function getAuditLogs(
|
export async function getAuditLogs(
|
||||||
@@ -15,7 +12,7 @@ export async function getAuditLogs(
|
|||||||
) {
|
) {
|
||||||
await requireBotGuildPermissions(guildId, ["VIEW_AUDIT_LOG"]);
|
await requireBotGuildPermissions(guildId, ["VIEW_AUDIT_LOG"]);
|
||||||
|
|
||||||
const result = await rest.runMethod(
|
return await rest.runMethod<AuditLog>(
|
||||||
"get",
|
"get",
|
||||||
endpoints.GUILD_AUDIT_LOGS(guildId),
|
endpoints.GUILD_AUDIT_LOGS(guildId),
|
||||||
camelKeysToSnakeCase({
|
camelKeysToSnakeCase({
|
||||||
@@ -25,6 +22,4 @@ export async function getAuditLogs(
|
|||||||
: 50,
|
: 50,
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
return snakeKeysToCamelCase<AuditLog>(result);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,5 @@ import { endpoints } from "../../util/constants.ts";
|
|||||||
|
|
||||||
/** Returns an array of voice regions that can be used when creating servers. */
|
/** Returns an array of voice regions that can be used when creating servers. */
|
||||||
export async function getAvailableVoiceRegions() {
|
export async function getAvailableVoiceRegions() {
|
||||||
const result = await rest.runMethod("get", endpoints.VOICE_REGIONS);
|
return await rest.runMethod<VoiceRegion>("get", endpoints.VOICE_REGIONS);
|
||||||
|
|
||||||
return result as VoiceRegion;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,16 +2,13 @@ import { rest } from "../../rest/rest.ts";
|
|||||||
import { Ban } from "../../types/guilds/ban.ts";
|
import { Ban } from "../../types/guilds/ban.ts";
|
||||||
import { endpoints } from "../../util/constants.ts";
|
import { endpoints } from "../../util/constants.ts";
|
||||||
import { requireBotGuildPermissions } from "../../util/permissions.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. */
|
/** 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) {
|
export async function getBan(guildId: string, memberId: string) {
|
||||||
await requireBotGuildPermissions(guildId, ["BAN_MEMBERS"]);
|
await requireBotGuildPermissions(guildId, ["BAN_MEMBERS"]);
|
||||||
|
|
||||||
const result = await rest.runMethod(
|
return await rest.runMethod<Ban>(
|
||||||
"get",
|
"get",
|
||||||
endpoints.GUILD_BAN(guildId, memberId),
|
endpoints.GUILD_BAN(guildId, memberId),
|
||||||
);
|
);
|
||||||
|
|
||||||
return snakeKeysToCamelCase<Ban>(result);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +1,19 @@
|
|||||||
import { rest } from "../../rest/rest.ts";
|
import { rest } from "../../rest/rest.ts";
|
||||||
import { Ban, DiscordBan } from "../../types/guilds/ban.ts";
|
import { Ban } from "../../types/guilds/ban.ts";
|
||||||
import { Collection } from "../../util/collection.ts";
|
import { Collection } from "../../util/collection.ts";
|
||||||
import { endpoints } from "../../util/constants.ts";
|
import { endpoints } from "../../util/constants.ts";
|
||||||
import { requireBotGuildPermissions } from "../../util/permissions.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. */
|
/** Returns a list of ban objects for the users banned from this guild. Requires the BAN_MEMBERS permission. */
|
||||||
export async function getBans(guildId: string) {
|
export async function getBans(guildId: string) {
|
||||||
await requireBotGuildPermissions(guildId, ["BAN_MEMBERS"]);
|
await requireBotGuildPermissions(guildId, ["BAN_MEMBERS"]);
|
||||||
|
|
||||||
const results = (await rest.runMethod(
|
const results = await rest.runMethod<Ban[]>(
|
||||||
"get",
|
"get",
|
||||||
endpoints.GUILD_BANS(guildId),
|
endpoints.GUILD_BANS(guildId),
|
||||||
)) as DiscordBan[];
|
);
|
||||||
|
|
||||||
return new Collection<string, Ban>(
|
return new Collection<string, Ban>(
|
||||||
results.map((res) => [res.user.id, snakeKeysToCamelCase<Ban>(res)]),
|
results.map((res) => [res.user.id, res]),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
|
import { cacheHandlers } from "../../cache.ts";
|
||||||
import { rest } from "../../rest/rest.ts";
|
import { rest } from "../../rest/rest.ts";
|
||||||
|
import { structures } from "../../structures/mod.ts";
|
||||||
import { Guild } from "../../types/guilds/guild.ts";
|
import { Guild } from "../../types/guilds/guild.ts";
|
||||||
import { endpoints } from "../../util/constants.ts";
|
import { endpoints } from "../../util/constants.ts";
|
||||||
import { snakeKeysToCamelCase } from "../../util/utils.ts";
|
import { ws } from "../../ws/ws.ts";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ⚠️ **If you need this, you are probably doing something wrong. Always use cache.guilds.get()
|
* ⚠️ **If you need this, you are probably doing something wrong. Always use cache.guilds.get()
|
||||||
@@ -10,10 +12,29 @@ import { snakeKeysToCamelCase } from "../../util/utils.ts";
|
|||||||
* This function fetches a guild's data. This is not the same data as a GUILD_CREATE.
|
* 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.
|
* So it does not cache the guild, you must do it manually.
|
||||||
* */
|
* */
|
||||||
export async function getGuild(guildId: string, counts = true) {
|
export async function getGuild(
|
||||||
const result = await rest.runMethod("get", endpoints.GUILDS_BASE(guildId), {
|
guildId: string,
|
||||||
with_counts: counts,
|
options: { counts?: boolean; addToCache?: boolean } = {
|
||||||
});
|
counts: true,
|
||||||
|
addToCache: true,
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
const result = await rest.runMethod<Guild>(
|
||||||
|
"get",
|
||||||
|
endpoints.GUILDS_BASE(guildId),
|
||||||
|
{
|
||||||
|
with_counts: options.counts,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
return snakeKeysToCamelCase<Guild>(result);
|
const structure = await structures.createDiscordenoGuild(
|
||||||
|
result,
|
||||||
|
(BigInt(guildId) >> 22n) % BigInt(ws.botGatewayData.shards),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (options.addToCache) {
|
||||||
|
await cacheHandlers.set("guilds", guildId, structure);
|
||||||
|
}
|
||||||
|
|
||||||
|
return structure;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import { rest } from "../../rest/rest.ts";
|
import { rest } from "../../rest/rest.ts";
|
||||||
import { GuildPreview } from "../../types/guilds/guild_preview.ts";
|
import { GuildPreview } from "../../types/guilds/guild_preview.ts";
|
||||||
import { endpoints } from "../../util/constants.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. */
|
/** 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) {
|
export async function getGuildPreview(guildId: string) {
|
||||||
const result = await rest.runMethod("get", endpoints.GUILD_PREVIEW(guildId));
|
return await rest.runMethod<GuildPreview>(
|
||||||
|
"get",
|
||||||
return snakeKeysToCamelCase<GuildPreview>(result);
|
endpoints.GUILD_PREVIEW(guildId),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,15 @@
|
|||||||
import { rest } from "../../rest/rest.ts";
|
import { rest } from "../../rest/rest.ts";
|
||||||
import { InviteMetadata } from "../../types/invites/invite_metadata.ts";
|
import { InviteMetadata } from "../../types/invites/invite_metadata.ts";
|
||||||
import { endpoints } from "../../util/constants.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 else `code` will be null. 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) {
|
export async function getVanityURL(guildId: string) {
|
||||||
const result = await rest.runMethod(
|
return await rest.runMethod<
|
||||||
"get",
|
|
||||||
endpoints.GUILD_VANITY_URL(guildId),
|
|
||||||
);
|
|
||||||
|
|
||||||
return snakeKeysToCamelCase<
|
|
||||||
(Partial<InviteMetadata> & Pick<InviteMetadata, "uses" | "code">) | {
|
(Partial<InviteMetadata> & Pick<InviteMetadata, "uses" | "code">) | {
|
||||||
code: null;
|
code: null;
|
||||||
}
|
}
|
||||||
>(result);
|
>(
|
||||||
|
"get",
|
||||||
|
endpoints.GUILD_VANITY_URL(guildId),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,10 @@
|
|||||||
import { rest } from "../../rest/rest.ts";
|
import { rest } from "../../rest/rest.ts";
|
||||||
import { WelcomeScreen } from "../../types/mod.ts";
|
import { WelcomeScreen } from "../../types/mod.ts";
|
||||||
import { endpoints } from "../../util/constants.ts";
|
import { endpoints } from "../../util/constants.ts";
|
||||||
import { snakeKeysToCamelCase } from "../../util/utils.ts";
|
|
||||||
|
|
||||||
export async function getWelcomeScreen(guildId: string) {
|
export async function getWelcomeScreen(guildId: string) {
|
||||||
const result = await rest.runMethod(
|
return await rest.runMethod<WelcomeScreen>(
|
||||||
"get",
|
"get",
|
||||||
endpoints.GUILD_WELCOME_SCREEN(guildId),
|
endpoints.GUILD_WELCOME_SCREEN(guildId),
|
||||||
);
|
);
|
||||||
|
|
||||||
return snakeKeysToCamelCase<WelcomeScreen>(result);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,5 +11,6 @@ export async function getWidget(guildId: string, options?: { force: boolean }) {
|
|||||||
if (!guild?.widgetEnabled) throw new Error(Errors.GUILD_WIDGET_NOT_ENABLED);
|
if (!guild?.widgetEnabled) throw new Error(Errors.GUILD_WIDGET_NOT_ENABLED);
|
||||||
}
|
}
|
||||||
|
|
||||||
return rest.runMethod("get", `${endpoints.GUILD_WIDGET(guildId)}.json`);
|
// TODO: add return type
|
||||||
|
return await rest.runMethod("get", `${endpoints.GUILD_WIDGET(guildId)}.json`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,8 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
|
|||||||
export async function getWidgetSettings(guildId: string) {
|
export async function getWidgetSettings(guildId: string) {
|
||||||
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
|
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
|
||||||
|
|
||||||
const result = await rest.runMethod("get", endpoints.GUILD_WIDGET(guildId));
|
return await rest.runMethod<GuildWidget>(
|
||||||
|
"get",
|
||||||
return result as GuildWidget;
|
endpoints.GUILD_WIDGET(guildId),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,9 @@ import { rest } from "../../rest/rest.ts";
|
|||||||
import { endpoints } from "../../util/constants.ts";
|
import { endpoints } from "../../util/constants.ts";
|
||||||
|
|
||||||
/** Leave a guild */
|
/** Leave a guild */
|
||||||
export async function leaveGuild(guildId: string): Promise<undefined> {
|
export async function leaveGuild(guildId: string) {
|
||||||
const result = await rest.runMethod("delete", endpoints.GUILD_LEAVE(guildId));
|
return await rest.runMethod<undefined>(
|
||||||
|
"delete",
|
||||||
return result;
|
endpoints.GUILD_LEAVE(guildId),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user