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:
Exists
2021-04-14 04:01:35 -04:00
parent f31216d0a4
commit 1a1ef34a96
5 changed files with 116 additions and 24 deletions
+28
View File
@@ -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
);
}