mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 19:28:17 +00:00
Add channel cloning
Changes:
- Added function cloneChannel
- Modified createChannel to support cloneChannel
- Added clone(reason) method on channel structure
- Added channel cloning tests
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
import { cacheHandlers } from "../../cache.ts";
|
||||
import { createChannel } from "./create_channel.ts";
|
||||
import { CreateGuildChannel } from "../../types/guilds/create_guild_channel.ts";
|
||||
import { DiscordenoChannel } from "../../structures/channel.ts";
|
||||
|
||||
/** Create a copy of a channel */
|
||||
export async function cloneChannel(channelId: string, reason?: string) {
|
||||
const channelToClone: DiscordenoChannel | undefined = await cacheHandlers.get(
|
||||
"channels",
|
||||
channelId
|
||||
);
|
||||
//Return undefined if channel is not cached (unsure about error handling)
|
||||
if (!channelToClone) return;
|
||||
|
||||
//If "name" is null or undefined as specified by types
|
||||
channelToClone.name ??= "new-channel";
|
||||
|
||||
//Merge channel data with reason for createChannel options
|
||||
const creationData = {
|
||||
reason,
|
||||
...channelToClone,
|
||||
};
|
||||
//Create the channel (also handles permissions)
|
||||
return createChannel(
|
||||
channelToClone.guildId!,
|
||||
creationData as CreateGuildChannel
|
||||
);
|
||||
}
|
||||
@@ -17,14 +17,22 @@ import { calculateBits } from "../../util/permissions.ts";
|
||||
/** Create a channel in your server. Bot needs MANAGE_CHANNEL permissions in the server. */
|
||||
export async function createChannel(
|
||||
guildId: string,
|
||||
options?: CreateGuildChannel,
|
||||
options?: CreateGuildChannel
|
||||
) {
|
||||
const requiredPerms: Set<PermissionStrings> = new Set(["MANAGE_CHANNELS"]);
|
||||
|
||||
//Integration with permissions for channel cloning
|
||||
let useDefaultOverwrites = false;
|
||||
|
||||
options?.permissionOverwrites?.forEach((overwrite) => {
|
||||
if (overwrite.id && parseInt(overwrite.allow)) {
|
||||
useDefaultOverwrites = true;
|
||||
|
||||
return;
|
||||
}
|
||||
eventHandlers.debug?.(
|
||||
"loop",
|
||||
`Running forEach loop in create_channel file.`,
|
||||
`Running forEach loop in create_channel file.`
|
||||
);
|
||||
overwrite.allow.forEach(requiredPerms.add, requiredPerms);
|
||||
overwrite.deny.forEach(requiredPerms.add, requiredPerms);
|
||||
@@ -40,14 +48,16 @@ export async function createChannel(
|
||||
endpoints.GUILD_CHANNELS(guildId),
|
||||
{
|
||||
...camelKeysToSnakeCase<DiscordCreateGuildChannel>(options ?? {}),
|
||||
permission_overwrites: options?.permissionOverwrites?.map((perm) => ({
|
||||
...perm,
|
||||
permission_overwrites: useDefaultOverwrites
|
||||
? options?.permissionOverwrites
|
||||
: options?.permissionOverwrites?.map((perm) => ({
|
||||
...perm,
|
||||
|
||||
allow: calculateBits(perm.allow),
|
||||
deny: calculateBits(perm.deny),
|
||||
})),
|
||||
allow: calculateBits(perm.allow),
|
||||
deny: calculateBits(perm.deny),
|
||||
})),
|
||||
type: options?.type || DiscordChannelTypes.GUILD_TEXT,
|
||||
},
|
||||
}
|
||||
)) as DiscordChannel;
|
||||
|
||||
const discordenoChannel = await structures.createDiscordenoChannel(result);
|
||||
|
||||
Reference in New Issue
Block a user