mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 11:28:15 +00:00
add: thread channel support
This commit is contained in:
@@ -2,28 +2,68 @@ import { eventHandlers } from "../../bot.ts";
|
|||||||
import { cacheHandlers } from "../../cache.ts";
|
import { cacheHandlers } from "../../cache.ts";
|
||||||
import { rest } from "../../rest/rest.ts";
|
import { rest } from "../../rest/rest.ts";
|
||||||
import { ModifyChannel } from "../../types/channels/modify_channel.ts";
|
import { ModifyChannel } from "../../types/channels/modify_channel.ts";
|
||||||
import { Channel } from "../../types/mod.ts";
|
import { ModifyThread } from "../../types/channels/threads/modify_thread.ts";
|
||||||
|
import {
|
||||||
|
Channel,
|
||||||
|
DiscordChannelTypes,
|
||||||
|
PermissionStrings,
|
||||||
|
} from "../../types/mod.ts";
|
||||||
import { endpoints } from "../../util/constants.ts";
|
import { endpoints } from "../../util/constants.ts";
|
||||||
import {
|
import {
|
||||||
calculateBits,
|
calculateBits,
|
||||||
|
requireBotChannelPermissions,
|
||||||
requireOverwritePermissions,
|
requireOverwritePermissions,
|
||||||
} from "../../util/permissions.ts";
|
} from "../../util/permissions.ts";
|
||||||
|
import { camelKeysToSnakeCase, hasOwnProperty } from "../../util/utils.ts";
|
||||||
|
|
||||||
//TODO: implement DM group channel edit
|
//TODO: implement DM group channel edit
|
||||||
/** 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(
|
export async function editChannel(
|
||||||
channelId: string,
|
channelId: string,
|
||||||
options: ModifyChannel,
|
options: ModifyChannel | ModifyThread,
|
||||||
reason?: string,
|
reason?: string,
|
||||||
) {
|
) {
|
||||||
const channel = await cacheHandlers.get("channels", channelId);
|
const channel = await cacheHandlers.get("channels", channelId);
|
||||||
if (channel?.guildId) {
|
|
||||||
await requireOverwritePermissions(
|
if (channel) {
|
||||||
channel.guildId,
|
if (
|
||||||
options.permissionOverwrites || [],
|
[
|
||||||
);
|
DiscordChannelTypes.GuildNewsThread,
|
||||||
|
DiscordChannelTypes.GuildPivateThread,
|
||||||
|
DiscordChannelTypes.GuildPublicThread,
|
||||||
|
].includes(channel.type)
|
||||||
|
) {
|
||||||
|
const permissions = new Set<PermissionStrings>();
|
||||||
|
|
||||||
|
if (hasOwnProperty(options, "archive") && options.archive === false) {
|
||||||
|
permissions.add("SEND_MESSAGES");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.name || options.topic) {
|
// 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 ?? "", [
|
||||||
|
...permissions,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
hasOwnProperty<ModifyChannel>(
|
||||||
|
options,
|
||||||
|
"permissionOverwrites",
|
||||||
|
) && Array.isArray(options.permissionOverwrites)
|
||||||
|
) {
|
||||||
|
await requireOverwritePermissions(
|
||||||
|
channel.guildId,
|
||||||
|
options.permissionOverwrites,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.name || (options as ModifyChannel).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
|
||||||
@@ -49,21 +89,18 @@ export async function editChannel(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const payload = {
|
const payload = {
|
||||||
...options,
|
...camelKeysToSnakeCase<{}>(options),
|
||||||
// deno-lint-ignore camelcase
|
// deno-lint-ignore camelcase
|
||||||
rate_limit_per_user: options.rateLimitPerUser,
|
permission_overwrites:
|
||||||
// deno-lint-ignore camelcase
|
hasOwnProperty<ModifyChannel>(options, "permissionOverwrites")
|
||||||
parent_id: options.parentId,
|
? options.permissionOverwrites?.map((overwrite) => {
|
||||||
// deno-lint-ignore camelcase
|
|
||||||
user_limit: options.userLimit,
|
|
||||||
// deno-lint-ignore camelcase
|
|
||||||
permission_overwrites: options.permissionOverwrites?.map((overwrite) => {
|
|
||||||
return {
|
return {
|
||||||
...overwrite,
|
...overwrite,
|
||||||
allow: calculateBits(overwrite.allow),
|
allow: calculateBits(overwrite.allow),
|
||||||
deny: calculateBits(overwrite.deny),
|
deny: calculateBits(overwrite.deny),
|
||||||
};
|
};
|
||||||
}),
|
})
|
||||||
|
: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
return await rest.runMethod<Channel>(
|
return await rest.runMethod<Channel>(
|
||||||
@@ -87,6 +124,7 @@ interface EditChannelRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const editChannelNameTopicQueue = new Map<string, EditChannelRequest>();
|
const editChannelNameTopicQueue = new Map<string, EditChannelRequest>();
|
||||||
|
|
||||||
let editChannelProcessing = false;
|
let editChannelProcessing = false;
|
||||||
|
|
||||||
function processEditChannelQueue() {
|
function processEditChannelQueue() {
|
||||||
|
|||||||
Reference in New Issue
Block a user