fix: bad channel.thread handling

This commit is contained in:
Skillz4Killz
2021-06-18 15:47:34 +00:00
committed by GitHub
parent 72f0836b7b
commit 5e4efd0f37
9 changed files with 53 additions and 44 deletions
+3 -2
View File
@@ -12,8 +12,6 @@ export async function deleteChannel(channelId: bigint, reason?: string) {
const guild = await cacheHandlers.get("guilds", channel.guildId); const guild = await cacheHandlers.get("guilds", channel.guildId);
if (!guild) throw new Error(Errors.GUILD_NOT_FOUND); if (!guild) throw new Error(Errors.GUILD_NOT_FOUND);
// TODO(threads): check if this requires guild perms or channel is enough
await requireBotGuildPermissions(guild, channel.isThreadChannel ? ["MANAGE_THREADS"] : ["MANAGE_CHANNELS"]);
if (guild.rulesChannelId === channelId) { if (guild.rulesChannelId === channelId) {
throw new Error(Errors.RULES_CHANNEL_CANNOT_BE_DELETED); throw new Error(Errors.RULES_CHANNEL_CANNOT_BE_DELETED);
} }
@@ -21,6 +19,9 @@ export async function deleteChannel(channelId: bigint, reason?: string) {
if (guild.publicUpdatesChannelId === channelId) { if (guild.publicUpdatesChannelId === channelId) {
throw new Error(Errors.UPDATES_CHANNEL_CANNOT_BE_DELETED); throw new Error(Errors.UPDATES_CHANNEL_CANNOT_BE_DELETED);
} }
// TODO(threads): check if this requires guild perms or channel is enough
await requireBotGuildPermissions(guild, ["MANAGE_CHANNELS"]);
} }
return await rest.runMethod<undefined>("delete", endpoints.CHANNEL_BASE(channelId), { reason }); return await rest.runMethod<undefined>("delete", endpoints.CHANNEL_BASE(channelId), { reason });
+6 -25
View File
@@ -5,41 +5,22 @@ import type { DiscordenoChannel } from "../../structures/channel.ts";
import { structures } from "../../structures/mod.ts"; import { structures } from "../../structures/mod.ts";
import type { Channel } from "../../types/channels/channel.ts"; import type { Channel } from "../../types/channels/channel.ts";
import type { ModifyChannel } from "../../types/channels/modify_channel.ts"; 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 { endpoints } from "../../util/constants.ts";
import { calculateBits, requireBotChannelPermissions, requireOverwritePermissions } from "../../util/permissions.ts"; import { calculateBits, requireOverwritePermissions } from "../../util/permissions.ts";
import { hasOwnProperty, snakelize } from "../../util/utils.ts"; import { snakelize } from "../../util/utils.ts";
//TODO: implement DM group channel edit //TODO: implement DM group channel edit
//TODO(threads): check thread perms
/** Update a channel's settings. Requires the `MANAGE_CHANNELS` permission for the guild. */ /** 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, reason?: string) {
const channel = await cacheHandlers.get("channels", channelId); const channel = await cacheHandlers.get("channels", channelId);
if (channel) { if (channel) {
if (channel.isThreadChannel) { if (options.permissionOverwrites && Array.isArray(options.permissionOverwrites)) {
const permissions = new Set<PermissionStrings>();
if (hasOwnProperty(options, "archive") && options.archive === false) {
permissions.add("SEND_MESSAGES");
}
// TODO(threads): change this to a better check
// hacky way of checking if more is being modified
if (Object.keys(options).length > 1) {
permissions.add("MANAGE_THREADS");
}
await requireBotChannelPermissions(channel.parentId ?? 0n, [...permissions]);
}
if (hasOwnProperty<ModifyChannel>(options, "permissionOverwrites") && Array.isArray(options.permissionOverwrites)) {
await requireOverwritePermissions(channel.guildId, options.permissionOverwrites); await requireOverwritePermissions(channel.guildId, options.permissionOverwrites);
} }
} }
if (options.name || (options as ModifyChannel).topic) { if (options.name || options.topic) {
const request = editChannelNameTopicQueue.get(channelId); const request = editChannelNameTopicQueue.get(channelId);
if (!request) { if (!request) {
// If this hasnt been done before simply add 1 for it // If this hasnt been done before simply add 1 for it
@@ -70,7 +51,7 @@ export async function editChannel(channelId: bigint, options: ModifyChannel | Mo
endpoints.CHANNEL_BASE(channelId), endpoints.CHANNEL_BASE(channelId),
snakelize({ snakelize({
...options, ...options,
permissionOverwrites: hasOwnProperty<ModifyChannel>(options, "permissionOverwrites") permissionOverwrites: options.permissionOverwrites
? options.permissionOverwrites?.map((overwrite) => { ? options.permissionOverwrites?.map((overwrite) => {
return { return {
...overwrite, ...overwrite,
@@ -1,6 +1,6 @@
import { editChannel } from "../edit_channel.ts"; import { editThread } from "./edit_thread.ts";
/** Sets a thread channel to be archived. */ /** Sets a thread channel to be archived. */
export function archiveThread(threadId: bigint) { export function archiveThread(threadId: bigint) {
return editChannel(threadId, { archived: true }); return editThread(threadId, { archived: true });
} }
@@ -0,0 +1,15 @@
import { cacheHandlers } from "../../../cache.ts";
import { rest } from "../../../rest/rest.ts";
import { endpoints } from "../../../util/constants.ts";
import { requireBotGuildPermissions } from "../../../util/permissions.ts";
/** Delete a thread in your server. Bot needs MANAGE_THREADS permissions in the server. */
export async function deleteThread(threadId: bigint, reason?: string) {
const thread = await cacheHandlers.get("threads", threadId);
if (thread) {
const channel = await cacheHandlers.get("channels", thread?.channelId);
if (channel?.guildId) await requireBotGuildPermissions(channel.guildId, ["MANAGE_THREADS"]);
}
return await rest.runMethod("delete", endpoints.CHANNEL_BASE(threadId), { reason });
}
@@ -0,0 +1,21 @@
import { rest } from "../../../rest/rest.ts";
import { ModifyThread } from "../../../types/channels/threads/modify_thread.ts";
import { endpoints } from "../../../util/constants.ts";
import { channelToThread } from "../../../util/transformers/channel_to_thread.ts";
import { snakelize } from "../../../util/utils.ts";
/** Update a thread's settings. Requires the `MANAGE_CHANNELS` permission for the guild. */
export async function editThread(threadId: bigint, options: ModifyThread, reason?: string) {
// const thread = await cacheHandlers.get("threads", threadId);
const result = await rest.runMethod(
"patch",
endpoints.CHANNEL_BASE(threadId),
snakelize({
...options,
reason,
})
);
return channelToThread(result);
}
+2 -2
View File
@@ -1,6 +1,6 @@
import { editChannel } from "../edit_channel.ts"; import { editThread } from "./edit_thread.ts";
/** Sets a thread channel to be locked. */ /** Sets a thread channel to be locked. */
export function lockThread(threadId: bigint) { export function lockThread(threadId: bigint) {
return editChannel(threadId, { locked: true }); return editThread(threadId, { locked: true });
} }
@@ -1,6 +1,6 @@
import { editChannel } from "../edit_channel.ts"; import { editThread } from "./edit_thread.ts";
/** Sets a thread channel to be unarchived. */ /** Sets a thread channel to be unarchived. */
export function unarchiveThread(threadId: bigint) { export function unarchiveThread(threadId: bigint) {
return editChannel(threadId, { archived: false }); return editThread(threadId, { archived: false });
} }
@@ -1,6 +1,6 @@
import { editChannel } from "../edit_channel.ts"; import { editThread } from "./edit_thread.ts";
/** Sets a thread channel to be unlocked. */ /** Sets a thread channel to be unlocked. */
export function unlockThread(threadId: bigint) { export function unlockThread(threadId: bigint) {
return editChannel(threadId, { locked: false }); return editThread(threadId, { locked: false });
} }
-9
View File
@@ -82,13 +82,6 @@ const baseChannel: Partial<DiscordenoChannel> = {
get isGuildTextBasedChannel() { get isGuildTextBasedChannel() {
return [DiscordChannelTypes.GuildNews, DiscordChannelTypes.GuildText].includes(this.type!); return [DiscordChannelTypes.GuildNews, DiscordChannelTypes.GuildText].includes(this.type!);
}, },
get isThreadChannel() {
return [
DiscordChannelTypes.GuildNewsThread,
DiscordChannelTypes.GuildPrivateThread,
DiscordChannelTypes.GuildPublicThread,
].includes(this.type!);
},
send(content) { send(content) {
return sendMessage(this.id!, content); return sendMessage(this.id!, content);
}, },
@@ -202,8 +195,6 @@ export interface DiscordenoChannel
isNewsChannel: boolean; isNewsChannel: boolean;
/** Whether the channel is a news or text channel in a guild. */ /** Whether the channel is a news or text channel in a guild. */
isGuildTextBasedChannel: boolean; isGuildTextBasedChannel: boolean;
/** Whether the channel is a thread type channel. */
isThreadChannel: boolean;
// METHODS // METHODS