Prettified Code!

This commit is contained in:
itohatweb
2021-05-21 15:55:49 +00:00
committed by GitHub Action
parent 71a20fb0f4
commit 179add27c9
271 changed files with 889 additions and 3067 deletions
+1 -4
View File
@@ -2,8 +2,5 @@ import { cacheHandlers } from "../../cache.ts";
/** Gets an array of all the channels ids that are the children of this category. */
export async function categoryChildren(id: bigint) {
return await cacheHandlers.filter(
"channels",
(channel) => channel.parentId === id
);
return await cacheHandlers.filter("channels", (channel) => channel.parentId === id);
}
@@ -13,9 +13,7 @@ export function channelOverwriteHasPermission(
})[],
permissions: PermissionStrings[]
) {
const overwrite =
overwrites.find((perm) => perm.id === id) ||
overwrites.find((perm) => perm.id === guildId);
const overwrite = overwrites.find((perm) => perm.id === id) || overwrites.find((perm) => perm.id === guildId);
if (!overwrite) return false;
+3 -12
View File
@@ -11,10 +11,7 @@ export async function cloneChannel(channelId: bigint, reason?: string) {
if (!channelToClone) throw new Error(Errors.CHANNEL_NOT_FOUND);
//Check for DM channel
if (
channelToClone.type === DiscordChannelTypes.DM ||
channelToClone.type === DiscordChannelTypes.GroupDm
) {
if (channelToClone.type === DiscordChannelTypes.DM || channelToClone.type === DiscordChannelTypes.GroupDm) {
throw new Error(Errors.CHANNEL_NOT_IN_GUILD);
}
@@ -22,9 +19,7 @@ export async function cloneChannel(channelId: bigint, reason?: string) {
...channelToClone,
name: channelToClone.name!,
topic: channelToClone.topic || undefined,
permissionOverwrites: channelToClone.permissionOverwrites.map((
overwrite,
) => ({
permissionOverwrites: channelToClone.permissionOverwrites.map((overwrite) => ({
id: overwrite.id.toString(),
type: overwrite.type,
allow: calculatePermissions(overwrite.allow.toString()),
@@ -33,9 +28,5 @@ export async function cloneChannel(channelId: bigint, reason?: string) {
};
//Create the channel (also handles permissions)
return await helpers.createChannel(
channelToClone.guildId!,
createChannelOptions,
reason,
);
return await helpers.createChannel(channelToClone.guildId!, createChannelOptions, reason);
}
+13 -27
View File
@@ -3,23 +3,13 @@ import { rest } from "../../rest/rest.ts";
import { structures } from "../../structures/mod.ts";
import type { Channel } from "../../types/channels/channel.ts";
import { DiscordChannelTypes } from "../../types/channels/channel_types.ts";
import type {
CreateGuildChannel,
DiscordCreateGuildChannel,
} from "../../types/guilds/create_guild_channel.ts";
import type { CreateGuildChannel, DiscordCreateGuildChannel } from "../../types/guilds/create_guild_channel.ts";
import { endpoints } from "../../util/constants.ts";
import {
calculateBits,
requireOverwritePermissions,
} from "../../util/permissions.ts";
import { calculateBits, requireOverwritePermissions } from "../../util/permissions.ts";
import { snakelize } from "../../util/utils.ts";
/** Create a channel in your server. Bot needs MANAGE_CHANNEL permissions in the server. */
export async function createChannel(
guildId: bigint,
options?: CreateGuildChannel,
reason?: string
) {
export async function createChannel(guildId: bigint, options?: CreateGuildChannel, reason?: string) {
if (options?.permissionOverwrites) {
await requireOverwritePermissions(guildId, options.permissionOverwrites);
}
@@ -27,20 +17,16 @@ export async function createChannel(
// BITRATES ARE IN THOUSANDS SO IF USER PROVIDES 32 WE CONVERT TO 32000
if (options?.bitrate && options.bitrate < 1000) options.bitrate *= 1000;
const result = await rest.runMethod<Channel>(
"post",
endpoints.GUILD_CHANNELS(guildId),
{
...snakelize<DiscordCreateGuildChannel>(options ?? {}),
permission_overwrites: options?.permissionOverwrites?.map((perm) => ({
...perm,
allow: calculateBits(perm.allow),
deny: calculateBits(perm.deny),
})),
type: options?.type || DiscordChannelTypes.GuildText,
reason,
}
);
const result = await rest.runMethod<Channel>("post", endpoints.GUILD_CHANNELS(guildId), {
...snakelize<DiscordCreateGuildChannel>(options ?? {}),
permission_overwrites: options?.permissionOverwrites?.map((perm) => ({
...perm,
allow: calculateBits(perm.allow),
deny: calculateBits(perm.deny),
})),
type: options?.type || DiscordChannelTypes.GuildText,
reason,
});
const discordenoChannel = await structures.createDiscordenoChannel(result);
await cacheHandlers.set("channels", discordenoChannel.id, discordenoChannel);
+5 -13
View File
@@ -16,23 +16,15 @@ export async function createStageInstance(channelId: bigint, topic: string) {
throw new Error(Errors.CHANNEL_NOT_STAGE_VOICE);
}
await requireBotChannelPermissions(channel, [
"MANAGE_CHANNELS",
"MUTE_MEMBERS",
"MOVE_MEMBERS",
]);
await requireBotChannelPermissions(channel, ["MANAGE_CHANNELS", "MUTE_MEMBERS", "MOVE_MEMBERS"]);
}
if (!validateLength(topic, { max: 120, min: 1 })) {
throw new Error(Errors.INVALID_TOPIC_LENGTH);
}
return await rest.runMethod<StageInstance>(
"post",
endpoints.STAGE_INSTANCES,
{
channel_id: channelId,
topic,
}
);
return await rest.runMethod<StageInstance>("post", endpoints.STAGE_INSTANCES, {
channel_id: channelId,
topic,
});
}
+4 -10
View File
@@ -16,11 +16,9 @@ export async function deleteChannel(channelId: bigint, reason?: string) {
// TODO(threads): check if this requires guild perms or channel is enough
await requireBotGuildPermissions(
guild,
[
ChannelTypes.GuildNewsThread,
ChannelTypes.GuildPivateThread,
ChannelTypes.GuildPublicThread,
].includes(channel.type)
[ChannelTypes.GuildNewsThread, ChannelTypes.GuildPivateThread, ChannelTypes.GuildPublicThread].includes(
channel.type
)
? ["MANAGE_THREADS"]
: ["MANAGE_CHANNELS"]
);
@@ -33,9 +31,5 @@ export async function deleteChannel(channelId: bigint, reason?: string) {
}
}
return await rest.runMethod<undefined>(
"delete",
endpoints.CHANNEL_BASE(channelId),
{ reason }
);
return await rest.runMethod<undefined>("delete", endpoints.CHANNEL_BASE(channelId), { reason });
}
@@ -10,8 +10,5 @@ export async function deleteChannelOverwrite(
): Promise<undefined> {
await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]);
return await rest.runMethod<undefined>(
"delete",
endpoints.CHANNEL_OVERWRITE(channelId, overwriteId)
);
return await rest.runMethod<undefined>("delete", endpoints.CHANNEL_OVERWRITE(channelId, overwriteId));
}
@@ -14,15 +14,8 @@ export async function deleteStageInstance(channelId: bigint) {
throw new Error(Errors.CHANNEL_NOT_STAGE_VOICE);
}
await requireBotChannelPermissions(channel, [
"MUTE_MEMBERS",
"MANAGE_CHANNELS",
"MOVE_MEMBERS",
]);
await requireBotChannelPermissions(channel, ["MUTE_MEMBERS", "MANAGE_CHANNELS", "MOVE_MEMBERS"]);
}
return await rest.runMethod<undefined>(
"delete",
endpoints.STAGE_INSTANCE(channelId)
);
return await rest.runMethod<undefined>("delete", endpoints.STAGE_INSTANCE(channelId));
}
+10 -33
View File
@@ -9,21 +9,13 @@ import type { ModifyChannel } from "../../types/channels/modify_channel.ts";
import type { ModifyThread } from "../../types/channels/threads/modify_thread.ts";
import type { PermissionStrings } from "../../types/permissions/permission_strings.ts";
import { endpoints } from "../../util/constants.ts";
import {
calculateBits,
requireBotChannelPermissions,
requireOverwritePermissions,
} from "../../util/permissions.ts";
import { calculateBits, requireBotChannelPermissions, requireOverwritePermissions } from "../../util/permissions.ts";
import { hasOwnProperty, snakelize } from "../../util/utils.ts";
//TODO: implement DM group channel edit
//TODO(threads): check thread perms
/** Update a channel's settings. Requires the `MANAGE_CHANNELS` permission for the guild. */
export async function editChannel(
channelId: bigint,
options: ModifyChannel | ModifyThread,
reason?: string
) {
export async function editChannel(channelId: bigint, options: ModifyChannel | ModifyThread, reason?: string) {
const channel = await cacheHandlers.get("channels", channelId);
if (channel) {
@@ -46,19 +38,11 @@ export async function editChannel(
permissions.add("MANAGE_THREADS");
}
await requireBotChannelPermissions(channel.parentId ?? 0n, [
...permissions,
]);
await requireBotChannelPermissions(channel.parentId ?? 0n, [...permissions]);
}
if (
hasOwnProperty<ModifyChannel>(options, "permissionOverwrites") &&
Array.isArray(options.permissionOverwrites)
) {
await requireOverwritePermissions(
channel.guildId,
options.permissionOverwrites
);
if (hasOwnProperty<ModifyChannel>(options, "permissionOverwrites") && Array.isArray(options.permissionOverwrites)) {
await requireOverwritePermissions(channel.guildId, options.permissionOverwrites);
}
}
@@ -91,10 +75,7 @@ export async function editChannel(
const payload = {
...snakelize<Record<string, unknown>>(options),
// deno-lint-ignore camelcase
permission_overwrites: hasOwnProperty<ModifyChannel>(
options,
"permissionOverwrites"
)
permission_overwrites: hasOwnProperty<ModifyChannel>(options, "permissionOverwrites")
? options.permissionOverwrites?.map((overwrite) => {
return {
...overwrite,
@@ -105,14 +86,10 @@ export async function editChannel(
: undefined,
};
const result = await rest.runMethod<Channel>(
"patch",
endpoints.CHANNEL_BASE(channelId),
{
...payload,
reason,
}
);
const result = await rest.runMethod<Channel>("patch", endpoints.CHANNEL_BASE(channelId), {
...payload,
reason,
});
return await structures.createDiscordenoChannel(result);
}
+6 -13
View File
@@ -1,10 +1,7 @@
import { rest } from "../../rest/rest.ts";
import type { Overwrite } from "../../types/channels/overwrite.ts";
import { endpoints } from "../../util/constants.ts";
import {
calculateBits,
requireBotGuildPermissions,
} from "../../util/permissions.ts";
import { calculateBits, requireBotGuildPermissions } from "../../util/permissions.ts";
/** Edit the channel permission overwrites for a user or role in this channel. Requires `MANAGE_ROLES` permission. */
export async function editChannelOverwrite(
@@ -15,13 +12,9 @@ export async function editChannelOverwrite(
): Promise<undefined> {
await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]);
return await rest.runMethod<undefined>(
"put",
endpoints.CHANNEL_OVERWRITE(channelId, overwriteId),
{
allow: calculateBits(options.allow),
deny: calculateBits(options.deny),
type: options.type,
}
);
return await rest.runMethod<undefined>("put", endpoints.CHANNEL_OVERWRITE(channelId, overwriteId), {
allow: calculateBits(options.allow),
deny: calculateBits(options.deny),
type: options.type,
});
}
+4 -11
View File
@@ -4,19 +4,12 @@ import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
/** Follow a News Channel to send messages to a target channel. Requires the `MANAGE_WEBHOOKS` permission in the target channel. Returns the webhook id. */
export async function followChannel(
sourceChannelId: bigint,
targetChannelId: bigint
) {
export async function followChannel(sourceChannelId: bigint, targetChannelId: bigint) {
await requireBotChannelPermissions(targetChannelId, ["MANAGE_WEBHOOKS"]);
const data = await rest.runMethod<FollowedChannel>(
"post",
endpoints.CHANNEL_FOLLOW(sourceChannelId),
{
webhook_channel_id: targetChannelId,
}
);
const data = await rest.runMethod<FollowedChannel>("post", endpoints.CHANNEL_FOLLOW(sourceChannelId), {
webhook_channel_id: targetChannelId,
});
return data.webhookId;
}
+2 -9
View File
@@ -10,21 +10,14 @@ import { endpoints } from "../../util/constants.ts";
* ⚠️ **If you need this, you are probably doing something wrong. This is not intended for use. Your channels will be cached in your guild.**
*/
export async function getChannel(channelId: bigint, addToCache = true) {
const result = await rest.runMethod<Channel>(
"get",
endpoints.CHANNEL_BASE(channelId)
);
const result = await rest.runMethod<Channel>("get", endpoints.CHANNEL_BASE(channelId));
const discordenoChannel = await structures.createDiscordenoChannel(
result,
result.guildId ? snowflakeToBigint(result.guildId) : undefined
);
if (addToCache) {
await cacheHandlers.set(
"channels",
discordenoChannel.id,
discordenoChannel
);
await cacheHandlers.set("channels", discordenoChannel.id, discordenoChannel);
}
return discordenoChannel;
+1 -4
View File
@@ -8,10 +8,7 @@ import { requireBotChannelPermissions } from "../../util/permissions.ts";
export async function getChannelWebhooks(channelId: bigint) {
await requireBotChannelPermissions(channelId, ["MANAGE_WEBHOOKS"]);
const result = await rest.runMethod<Webhook[]>(
"get",
endpoints.CHANNEL_WEBHOOKS(channelId)
);
const result = await rest.runMethod<Webhook[]>("get", endpoints.CHANNEL_WEBHOOKS(channelId));
return new Collection(result.map((webhook) => [webhook.id, webhook]));
}
+3 -13
View File
@@ -10,25 +10,15 @@ import { endpoints } from "../../util/constants.ts";
* ⚠️ **If you need this, you are probably doing something wrong. This is not intended for use. Your channels will be cached in your guild.**
*/
export async function getChannels(guildId: bigint, addToCache = true) {
const result = await rest.runMethod<Channel[]>(
"get",
endpoints.GUILD_CHANNELS(guildId)
);
const result = await rest.runMethod<Channel[]>("get", endpoints.GUILD_CHANNELS(guildId));
return new Collection(
(
await Promise.all(
result.map(async (res) => {
const discordenoChannel = await structures.createDiscordenoChannel(
res,
guildId
);
const discordenoChannel = await structures.createDiscordenoChannel(res, guildId);
if (addToCache) {
await cacheHandlers.set(
"channels",
discordenoChannel.id,
discordenoChannel
);
await cacheHandlers.set("channels", discordenoChannel.id, discordenoChannel);
}
return discordenoChannel;
+2 -7
View File
@@ -5,12 +5,7 @@ import { endpoints } from "../../util/constants.ts";
/** Get pinned messages in this channel. */
export async function getPins(channelId: bigint) {
const result = await rest.runMethod<Message[]>(
"get",
endpoints.CHANNEL_PINS(channelId)
);
const result = await rest.runMethod<Message[]>("get", endpoints.CHANNEL_PINS(channelId));
return Promise.all(
result.map((res) => structures.createDiscordenoMessage(res))
);
return Promise.all(result.map((res) => structures.createDiscordenoMessage(res)));
}
+1 -4
View File
@@ -15,8 +15,5 @@ export async function getStageInstance(channelId: bigint) {
}
}
return await rest.runMethod<StageInstance>(
"get",
endpoints.STAGE_INSTANCE(channelId)
);
return await rest.runMethod<StageInstance>("get", endpoints.STAGE_INSTANCE(channelId));
}
+2 -6
View File
@@ -9,12 +9,8 @@ export async function isChannelSynced(channelId: bigint) {
if (!parentChannel) return false;
return channel.permissionOverwrites?.every((overwrite) => {
const permission = parentChannel.permissionOverwrites?.find(
(ow) => ow.id === overwrite.id
);
const permission = parentChannel.permissionOverwrites?.find((ow) => ow.id === overwrite.id);
if (!permission) return false;
return !(
overwrite.allow !== permission.allow || overwrite.deny !== permission.deny
);
return !(overwrite.allow !== permission.allow || overwrite.deny !== permission.deny);
});
}
+2 -7
View File
@@ -27,16 +27,11 @@ export async function startTyping(channelId: bigint) {
throw new Error(Errors.CHANNEL_NOT_TEXT_BASED);
}
const hasSendMessagesPerm = await botHasChannelPermissions(channelId, [
"SEND_MESSAGES",
]);
const hasSendMessagesPerm = await botHasChannelPermissions(channelId, ["SEND_MESSAGES"]);
if (!hasSendMessagesPerm) {
throw new Error(Errors.MISSING_SEND_MESSAGES);
}
}
return await rest.runMethod<undefined>(
"post",
endpoints.CHANNEL_TYPING(channelId)
);
return await rest.runMethod<undefined>("post", endpoints.CHANNEL_TYPING(channelId));
}
+2 -9
View File
@@ -3,17 +3,10 @@ import type { ModifyGuildChannelPositions } from "../../types/guilds/modify_guil
import { endpoints } from "../../util/constants.ts";
/** Modify the positions of channels on the guild. Requires MANAGE_CHANNELS permisison. */
export async function swapChannels(
guildId: bigint,
channelPositions: ModifyGuildChannelPositions[]
) {
export async function swapChannels(guildId: bigint, channelPositions: ModifyGuildChannelPositions[]) {
if (channelPositions.length < 2) {
throw "You must provide at least two channels to be swapped.";
}
return await rest.runMethod<undefined>(
"patch",
endpoints.GUILD_CHANNELS(guildId),
channelPositions
);
return await rest.runMethod<undefined>("patch", endpoints.GUILD_CHANNELS(guildId), channelPositions);
}
@@ -12,11 +12,9 @@ export async function addToThread(channelId: bigint, userId?: bigint) {
const channel = await cacheHandlers.get("channels", channelId);
if (channel) {
if (
![
ChannelTypes.GuildNewsThread,
ChannelTypes.GuildPivateThread,
ChannelTypes.GuildPublicThread,
].includes(channel.type)
![ChannelTypes.GuildNewsThread, ChannelTypes.GuildPivateThread, ChannelTypes.GuildPublicThread].includes(
channel.type
)
) {
throw new Error(Errors.NOT_A_THREAD_CHANNEL);
}
@@ -24,8 +22,6 @@ export async function addToThread(channelId: bigint, userId?: bigint) {
return await rest.runMethod(
"put",
userId
? endpoints.THREAD_USER(channelId, userId)
: endpoints.THREAD_ME(channelId)
userId ? endpoints.THREAD_USER(channelId, userId) : endpoints.THREAD_ME(channelId)
);
}
@@ -12,11 +12,9 @@ export async function getThreadMembers(channelId: bigint) {
const channel = await cacheHandlers.get("channels", channelId);
if (channel) {
if (
![
ChannelTypes.GuildNewsThread,
ChannelTypes.GuildPivateThread,
ChannelTypes.GuildPublicThread,
].includes(channel.type)
![ChannelTypes.GuildNewsThread, ChannelTypes.GuildPivateThread, ChannelTypes.GuildPublicThread].includes(
channel.type
)
) {
throw new Error(Errors.NOT_A_THREAD_CHANNEL);
}
@@ -10,11 +10,9 @@ export async function removeFromThread(channelId: bigint, userId?: bigint) {
const channel = await cacheHandlers.get("channels", channelId);
if (channel) {
if (
![
ChannelTypes.GuildNewsThread,
ChannelTypes.GuildPivateThread,
ChannelTypes.GuildPublicThread,
].includes(channel.type)
![ChannelTypes.GuildNewsThread, ChannelTypes.GuildPivateThread, ChannelTypes.GuildPublicThread].includes(
channel.type
)
) {
throw new Error(Errors.NOT_A_THREAD_CHANNEL);
}
@@ -22,8 +20,6 @@ export async function removeFromThread(channelId: bigint, userId?: bigint) {
return await rest.runMethod(
"delete",
userId
? endpoints.THREAD_USER(channelId, userId)
: endpoints.THREAD_ME(channelId)
userId ? endpoints.THREAD_USER(channelId, userId) : endpoints.THREAD_ME(channelId)
);
}
+2 -7
View File
@@ -10,16 +10,11 @@ import { snakelize } from "../../../util/utils.ts";
* Creates a new public thread from an existing message. Returns a channel on success, and a 400 BAD REQUEST on invalid parameters. Fires a Thread Create Gateway event.
* @param messageId when provided the thread will be public
*/
export async function startThread(
channelId: bigint,
options: StartThread & { messageId?: bigint }
) {
export async function startThread(channelId: bigint, options: StartThread & { messageId?: bigint }) {
const channel = await cacheHandlers.get("channels", channelId);
if (channel) {
// TODO(threads): perm check
if (
![ChannelTypes.GuildText, ChannelTypes.GuildNews].includes(channel.type)
) {
if (![ChannelTypes.GuildText, ChannelTypes.GuildNews].includes(channel.type)) {
throw new Error(Errors.INVALID_THREAD_PARENT_CHANNEL_TYPE);
}
+4 -12
View File
@@ -16,11 +16,7 @@ export async function updateStageInstance(channelId: bigint, topic: string) {
throw new Error(Errors.CHANNEL_NOT_STAGE_VOICE);
}
await requireBotChannelPermissions(channel, [
"MOVE_MEMBERS",
"MUTE_MEMBERS",
"MANAGE_CHANNELS",
]);
await requireBotChannelPermissions(channel, ["MOVE_MEMBERS", "MUTE_MEMBERS", "MANAGE_CHANNELS"]);
}
if (
@@ -32,11 +28,7 @@ export async function updateStageInstance(channelId: bigint, topic: string) {
throw new Error(Errors.INVALID_TOPIC_LENGTH);
}
return await rest.runMethod<StageInstance>(
"patch",
endpoints.STAGE_INSTANCE(channelId),
{
topic,
}
);
return await rest.runMethod<StageInstance>("patch", endpoints.STAGE_INSTANCE(channelId), {
topic,
});
}
+1 -4
View File
@@ -21,10 +21,7 @@ export async function updateBotVoiceState(
) {
return await rest.runMethod(
"patch",
endpoints.UPDATE_VOICE_STATE(
guildId,
hasOwnProperty(options, "userId") ? options.userId : undefined
),
endpoints.UPDATE_VOICE_STATE(guildId, hasOwnProperty(options, "userId") ? options.userId : undefined),
snakelize(options)
);
}