This snippet will create a channel in a specified category (if category is not existing, will automatically create one, create a new category). Bot will message the creator after successful creation, and will provide a button to directly navigate to the new channel. Note that access is initially set as private, only the creator can view it! -- customizable see comment at line 40
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const categoryName = `custom-channels`; //Category name where channels will be created [You can change this]
const guildId = context.params.event.guild_id;
let categoryId = ''; //Placeholder for the category
//Get all existing channels in the discord server
let channels = await lib.discord.guilds['@0.0.2'].channels.list({
guild_id: guildId,
});
//Find the target category based on categoryName
let targetCategory = channels.find((channel) => {
return channel.name.toLowerCase().indexOf(categoryName.toLowerCase()) > -1;
});
//Check if the target category is existing, else create a new category
if (targetCategory) {
categoryId = targetCategory.id;
} else {
let newCategory = await lib.discord.guilds['@release'].channels.create({
guild_id: guildId,
name: categoryName,
type: 4, //type = 4 for category
});
categoryId = newCategory.id;
}
//Create a new channel wherein access is not allowed to everyone but only for the user who created it
let newChannel = await lib.discord.guilds['@0.2.4'].channels.create({
guild_id: guildId,
parent_id: categoryId,
name: `${context.params.event.author.username}-channel`, //Name of the new channel [You can change this]
topic: ``,
type: 0,
permission_overwrites: [
{
id: `${context.params.event.guild_id}`,
type: 0,
deny: `${1 << 10}`, //change "deny" to "allow" if you want everyone to access the new channel
},
{
id: `${context.params.event.author.id}`,
type: 1,
allow: `${1 << 10}`,
},
],
});
//Get the url for newly created channel
let channelUrl =
'https://discord.com/channels/' + guildId + '/' + newChannel.id;
//Create a message with button to navigate to the new channel
return lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: ``,
components: [
{
type: 1,
components: [
{
style: 5,
label: `Go to channel`,
url: channelUrl,
disabled: false,
type: 2,
},
],
},
],
embeds: [
{
type: 'rich',
title: `New Channel`,
description: `Hey, <@!${context.params.event.author.id}>! Your channel has been successfully created.`,
color: 0x00ffff,
},
],
});