Prettified Code!

This commit is contained in:
itohatweb
2021-05-21 15:51:33 +00:00
committed by GitHub Action
parent fe4ab8517b
commit 3549cdeb63
279 changed files with 1617 additions and 1768 deletions
+1 -1
View File
@@ -4,6 +4,6 @@ import { cacheHandlers } from "../../cache.ts";
export async function categoryChildren(id: bigint) {
return await cacheHandlers.filter(
"channels",
(channel) => channel.parentId === id,
(channel) => channel.parentId === id
);
}
@@ -11,9 +11,10 @@ export function channelOverwriteHasPermission(
allow: bigint;
deny: bigint;
})[],
permissions: PermissionStrings[],
permissions: PermissionStrings[]
) {
const overwrite = overwrites.find((perm) => perm.id === id) ||
const overwrite =
overwrites.find((perm) => perm.id === id) ||
overwrites.find((perm) => perm.id === guildId);
if (!overwrite) return false;
@@ -21,14 +22,10 @@ export function channelOverwriteHasPermission(
return permissions.every((perm) => {
const allowBits = overwrite.allow;
const denyBits = overwrite.deny;
if (
BigInt(denyBits) & BigInt(DiscordBitwisePermissionFlags[perm])
) {
if (BigInt(denyBits) & BigInt(DiscordBitwisePermissionFlags[perm])) {
return false;
}
if (
BigInt(allowBits) & BigInt(DiscordBitwisePermissionFlags[perm])
) {
if (BigInt(allowBits) & BigInt(DiscordBitwisePermissionFlags[perm])) {
return true;
}
});
+9 -9
View File
@@ -26,20 +26,20 @@ export async function cloneChannel(channelId: bigint, reason?: string) {
parentId: channelToClone.parentId
? bigintToSnowflake(channelToClone.parentId)
: undefined,
permissionOverwrites: channelToClone.permissionOverwrites.map((
overwrite,
) => ({
id: overwrite.id.toString(),
type: overwrite.type,
allow: calculatePermissions(overwrite.allow.toString()),
deny: calculatePermissions(overwrite.deny.toString()),
})),
permissionOverwrites: channelToClone.permissionOverwrites.map(
(overwrite) => ({
id: overwrite.id.toString(),
type: overwrite.type,
allow: calculatePermissions(overwrite.allow.toString()),
deny: calculatePermissions(overwrite.deny.toString()),
})
),
};
//Create the channel (also handles permissions)
return await helpers.createChannel(
channelToClone.guildId!,
createChannelOptions,
reason,
reason
);
}
+3 -6
View File
@@ -18,13 +18,10 @@ import { snakelize } from "../../util/utils.ts";
export async function createChannel(
guildId: bigint,
options?: CreateGuildChannel,
reason?: string,
reason?: string
) {
if (options?.permissionOverwrites) {
await requireOverwritePermissions(
guildId,
options.permissionOverwrites,
);
await requireOverwritePermissions(guildId, options.permissionOverwrites);
}
// BITRATES ARE IN THOUSANDS SO IF USER PROVIDES 32 WE CONVERT TO 32000
@@ -42,7 +39,7 @@ export async function createChannel(
})),
type: options?.type || DiscordChannelTypes.GuildText,
reason,
},
}
);
const discordenoChannel = await structures.createDiscordenoChannel(result);
@@ -23,9 +23,7 @@ export async function createStageInstance(channelId: bigint, topic: string) {
]);
}
if (
!validateLength(topic, { max: 120, min: 1 })
) {
if (!validateLength(topic, { max: 120, min: 1 })) {
throw new Error(Errors.INVALID_TOPIC_LENGTH);
}
@@ -33,8 +31,8 @@ export async function createStageInstance(channelId: bigint, topic: string) {
"post",
endpoints.STAGE_INSTANCES,
{
"channel_id": channelId,
channel_id: channelId,
topic,
},
}
);
}
+7 -10
View File
@@ -6,10 +6,7 @@ import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
/** Delete a channel in your server. Bot needs MANAGE_CHANNEL permissions in the server. */
export async function deleteChannel(
channelId: bigint,
reason?: string,
) {
export async function deleteChannel(channelId: bigint, reason?: string) {
const channel = await cacheHandlers.get("channels", channelId);
if (channel?.guildId) {
@@ -20,12 +17,12 @@ export async function deleteChannel(
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"],
: ["MANAGE_CHANNELS"]
);
if (guild.rulesChannelId === channelId) {
throw new Error(Errors.RULES_CHANNEL_CANNOT_BE_DELETED);
@@ -39,6 +36,6 @@ export async function deleteChannel(
return await rest.runMethod<undefined>(
"delete",
endpoints.CHANNEL_BASE(channelId),
{ reason },
{ reason }
);
}
@@ -6,12 +6,12 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
export async function deleteChannelOverwrite(
guildId: bigint,
channelId: bigint,
overwriteId: bigint,
overwriteId: bigint
): Promise<undefined> {
await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]);
return await rest.runMethod<undefined>(
"delete",
endpoints.CHANNEL_OVERWRITE(channelId, overwriteId),
endpoints.CHANNEL_OVERWRITE(channelId, overwriteId)
);
}
@@ -23,6 +23,6 @@ export async function deleteStageInstance(channelId: bigint) {
return await rest.runMethod<undefined>(
"delete",
endpoints.STAGE_INSTANCE(channelId),
endpoints.STAGE_INSTANCE(channelId)
);
}
+12 -12
View File
@@ -22,7 +22,7 @@ import { hasOwnProperty, snakelize } from "../../util/utils.ts";
export async function editChannel(
channelId: bigint,
options: ModifyChannel | ModifyThread,
reason?: string,
reason?: string
) {
const channel = await cacheHandlers.get("channels", channelId);
@@ -57,7 +57,7 @@ export async function editChannel(
) {
await requireOverwritePermissions(
channel.guildId,
options.permissionOverwrites,
options.permissionOverwrites
);
}
}
@@ -92,16 +92,16 @@ export async function editChannel(
...snakelize<Record<string, unknown>>(options),
// deno-lint-ignore camelcase
permission_overwrites: hasOwnProperty<ModifyChannel>(
options,
"permissionOverwrites",
)
options,
"permissionOverwrites"
)
? options.permissionOverwrites?.map((overwrite) => {
return {
...overwrite,
allow: calculateBits(overwrite.allow),
deny: calculateBits(overwrite.deny),
};
})
return {
...overwrite,
allow: calculateBits(overwrite.allow),
deny: calculateBits(overwrite.deny),
};
})
: undefined,
};
@@ -111,7 +111,7 @@ export async function editChannel(
{
...payload,
reason,
},
}
);
return await structures.createDiscordenoChannel(result);
@@ -11,7 +11,7 @@ export async function editChannelOverwrite(
guildId: bigint,
channelId: bigint,
overwriteId: bigint,
options: Omit<Overwrite, "id">,
options: Omit<Overwrite, "id">
): Promise<undefined> {
await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]);
@@ -22,6 +22,6 @@ export async function editChannelOverwrite(
allow: calculateBits(options.allow),
deny: calculateBits(options.deny),
type: options.type,
},
}
);
}
+2 -2
View File
@@ -6,7 +6,7 @@ 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,
targetChannelId: bigint
) {
await requireBotChannelPermissions(targetChannelId, ["MANAGE_WEBHOOKS"]);
@@ -15,7 +15,7 @@ export async function followChannel(
endpoints.CHANNEL_FOLLOW(sourceChannelId),
{
webhook_channel_id: targetChannelId,
},
}
);
return data.webhookId;
+3 -3
View File
@@ -12,18 +12,18 @@ import { endpoints } from "../../util/constants.ts";
export async function getChannel(channelId: bigint, addToCache = true) {
const result = await rest.runMethod<Channel>(
"get",
endpoints.CHANNEL_BASE(channelId),
endpoints.CHANNEL_BASE(channelId)
);
const discordenoChannel = await structures.createDiscordenoChannel(
result,
result.guildId ? snowflakeToBigint(result.guildId) : undefined,
result.guildId ? snowflakeToBigint(result.guildId) : undefined
);
if (addToCache) {
await cacheHandlers.set(
"channels",
discordenoChannel.id,
discordenoChannel,
discordenoChannel
);
}
+2 -4
View File
@@ -10,10 +10,8 @@ export async function getChannelWebhooks(channelId: bigint) {
const result = await rest.runMethod<Webhook[]>(
"get",
endpoints.CHANNEL_WEBHOOKS(channelId),
endpoints.CHANNEL_WEBHOOKS(channelId)
);
return new Collection(
result.map((webhook) => [webhook.id, webhook]),
);
return new Collection(result.map((webhook) => [webhook.id, webhook]));
}
+5 -5
View File
@@ -12,7 +12,7 @@ import { endpoints } from "../../util/constants.ts";
export async function getChannels(guildId: bigint, addToCache = true) {
const result = await rest.runMethod<Channel[]>(
"get",
endpoints.GUILD_CHANNELS(guildId),
endpoints.GUILD_CHANNELS(guildId)
);
return new Collection(
@@ -21,19 +21,19 @@ export async function getChannels(guildId: bigint, addToCache = true) {
result.map(async (res) => {
const discordenoChannel = await structures.createDiscordenoChannel(
res,
guildId,
guildId
);
if (addToCache) {
await cacheHandlers.set(
"channels",
discordenoChannel.id,
discordenoChannel,
discordenoChannel
);
}
return discordenoChannel;
}),
})
)
).map((c) => [c.id, c]),
).map((c) => [c.id, c])
);
}
+2 -2
View File
@@ -7,10 +7,10 @@ import { endpoints } from "../../util/constants.ts";
export async function getPins(channelId: bigint) {
const result = await rest.runMethod<Message[]>(
"get",
endpoints.CHANNEL_PINS(channelId),
endpoints.CHANNEL_PINS(channelId)
);
return Promise.all(
result.map((res) => structures.createDiscordenoMessage(res)),
result.map((res) => structures.createDiscordenoMessage(res))
);
}
+1 -1
View File
@@ -17,6 +17,6 @@ export async function getStageInstance(channelId: bigint) {
return await rest.runMethod<StageInstance>(
"get",
endpoints.STAGE_INSTANCE(channelId),
endpoints.STAGE_INSTANCE(channelId)
);
}
+1 -1
View File
@@ -10,7 +10,7 @@ export async function isChannelSynced(channelId: bigint) {
return channel.permissionOverwrites?.every((overwrite) => {
const permission = parentChannel.permissionOverwrites?.find(
(ow) => ow.id === overwrite.id,
(ow) => ow.id === overwrite.id
);
if (!permission) return false;
return !(
+5 -8
View File
@@ -27,19 +27,16 @@ export async function startTyping(channelId: bigint) {
throw new Error(Errors.CHANNEL_NOT_TEXT_BASED);
}
const hasSendMessagesPerm = await botHasChannelPermissions(
channelId,
["SEND_MESSAGES"],
);
if (
!hasSendMessagesPerm
) {
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),
endpoints.CHANNEL_TYPING(channelId)
);
}
+2 -2
View File
@@ -5,7 +5,7 @@ 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[],
channelPositions: ModifyGuildChannelPositions[]
) {
if (channelPositions.length < 2) {
throw "You must provide at least two channels to be swapped.";
@@ -14,6 +14,6 @@ export async function swapChannels(
return await rest.runMethod<undefined>(
"patch",
endpoints.GUILD_CHANNELS(guildId),
channelPositions,
channelPositions
);
}
@@ -26,6 +26,6 @@ export async function addToThread(channelId: bigint, userId?: bigint) {
"put",
userId
? endpoints.THREAD_USER(channelId, userId)
: endpoints.THREAD_ME(channelId),
: endpoints.THREAD_ME(channelId)
);
}
@@ -5,8 +5,5 @@ import { endpoints } from "../../../util/constants.ts";
export async function getActiveThreads(channelId: bigint) {
// TODO(threads): perm check
// TODO(threads): test if it works
return await rest.runMethod(
"get",
endpoints.THREAD_ACTIVE(channelId),
);
return await rest.runMethod("get", endpoints.THREAD_ACTIVE(channelId));
}
@@ -7,7 +7,7 @@ export async function getArchivedThreads(
channelId: bigint,
options?: ListPublicArchivedThreads & {
type?: "public" | "private" | "privateJoinedThreads";
},
}
) {
// TODO(threads): perm check
// TODO(threads): check if this works
@@ -19,6 +19,6 @@ export async function getArchivedThreads(
: options?.type === "private"
? endpoints.THREAD_ARCHIVED_PRIVATE(channelId)
: endpoints.THREAD_ARCHIVED_PUBLIC(channelId),
snakelize(options ?? {}),
snakelize(options ?? {})
);
}
@@ -24,6 +24,6 @@ export async function removeFromThread(channelId: bigint, userId?: bigint) {
"delete",
userId
? endpoints.THREAD_USER(channelId, userId)
: endpoints.THREAD_ME(channelId),
: endpoints.THREAD_ME(channelId)
);
}
+2 -2
View File
@@ -12,7 +12,7 @@ import { snakelize } from "../../../util/utils.ts";
*/
export async function startThread(
channelId: bigint,
options: StartThread & { messageId?: bigint },
options: StartThread & { messageId?: bigint }
) {
const channel = await cacheHandlers.get("channels", channelId);
if (channel) {
@@ -33,6 +33,6 @@ export async function startThread(
options?.messageId
? endpoints.THREAD_START_PUBLIC(channelId, options.messageId)
: endpoints.THREAD_START_PRIVATE(channelId),
snakelize(options),
snakelize(options)
);
}
@@ -37,6 +37,6 @@ export async function updateStageInstance(channelId: bigint, topic: string) {
endpoints.STAGE_INSTANCE(channelId),
{
topic,
},
}
);
}
+4 -6
View File
@@ -1,8 +1,6 @@
import { rest } from "../../rest/rest.ts";
import { UpdateOthersVoiceState } from "../../types/guilds/update_others_voice_state.ts";
import type {
UpdateSelfVoiceState,
} from "../../types/guilds/update_self_voice_state.ts";
import type { UpdateSelfVoiceState } from "../../types/guilds/update_self_voice_state.ts";
import { endpoints } from "../../util/constants.ts";
import { hasOwnProperty, snakelize } from "../../util/utils.ts";
@@ -19,14 +17,14 @@ import { hasOwnProperty, snakelize } from "../../util/utils.ts";
*/
export async function updateBotVoiceState(
guildId: bigint,
options: UpdateSelfVoiceState | { userId: bigint } & UpdateOthersVoiceState,
options: UpdateSelfVoiceState | ({ userId: bigint } & UpdateOthersVoiceState)
) {
return await rest.runMethod(
"patch",
endpoints.UPDATE_VOICE_STATE(
guildId,
hasOwnProperty(options, "userId") ? options.userId : undefined,
hasOwnProperty(options, "userId") ? options.userId : undefined
),
snakelize(options),
snakelize(options)
);
}