How to create a new channel for a guild?

I have a bot that will create a text channel to write into when invited. My question is how can I do this with discord.js? More specifically. I have this code that I have written by just exploring the API:
const guild = await client.guilds.cache.get("<some-id>");
guild?.channels.create({
name: "test",
type: ChannelType.GuildText,
topic: "test",
});
const guild = await client.guilds.cache.get("<some-id>");
guild?.channels.create({
name: "test",
type: ChannelType.GuildText,
topic: "test",
});
I'm guessing that this will work, but I'm not sure I understand what cache is about. I've seen this throughout the API, but I'm not sure how it works. Do I have to load the cache, or does it happen lazily whenever I try to interact with something?
46 Replies
d.js toolkit
d.js toolkitā€¢15mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
addamsson
addamssonOPā€¢15mo ago
Alternatively it would also help a lot if somebody pointed me to how caching works (I didn't find explicit guidance regarding all the cache properties in the library, but I might have missed it).
Unknown User
Unknown Userā€¢15mo ago
Message Not Public
Sign In & Join Server To View
treble/luna
treble/lunaā€¢15mo ago
i also recommend taking a different approach since 1) its kinda bad UX and 2) your bot might not always have perms to create channels
Unknown User
Unknown Userā€¢15mo ago
Message Not Public
Sign In & Join Server To View
treble/luna
treble/lunaā€¢15mo ago
why would you want to create a new channel? just make a command like /help or /setup plus the channel isnt perm guarded
addamsson
addamssonOPā€¢15mo ago
because it is part of the onboarding process either if you start it from the /setup command or the web client i already have /setup šŸ˜… either way I'll need the channel which is the bad UX part .... aaaaand what is the different approach? makes sense. it is not documented (or at least I don't see the documentation when I browse the code) but it seems that fetch will return a Guild so I guess if no guild is found it will throw? This is my updated code:
const guild = await client.guilds.fetch("<some-id>");
guild?.channels.create({
name: "test",
type: ChannelType.GuildText,
topic: "test",
});
const guild = await client.guilds.fetch("<some-id>");
guild?.channels.create({
name: "test",
type: ChannelType.GuildText,
topic: "test",
});
I just noted that ? is not required here as the signature doesn't have undefined as a possible return value
treble/luna
treble/lunaā€¢15mo ago
something else than creating a channel no need to fetch, all guilds are cached by the Guilds intent
addamsson
addamssonOPā€¢15mo ago
why is creating a channel bad ux? you don't even know the use case šŸ˜…
treble/luna
treble/lunaā€¢15mo ago
is it a public bot? because you're not setting perms for the channel thus allowing everyone to see and send messages to it
addamsson
addamssonOPā€¢15mo ago
I'm a bit confused here. @thee2d said that it might not be available so to make sure that I have data I need to fetch. or not? yes
treble/luna
treble/lunaā€¢15mo ago
all guilds, channels and roles are cached by the Guilds intent
Unknown User
Unknown Userā€¢15mo ago
Message Not Public
Sign In & Join Server To View
treble/luna
treble/lunaā€¢15mo ago
for members you would need to fetch
addamsson
addamssonOPā€¢15mo ago
what does "cached by the Guilds intent" mean?
treble/luna
treble/lunaā€¢15mo ago
exactly as says
addamsson
addamssonOPā€¢15mo ago
also there needs to be a fetch operation at some point
treble/luna
treble/lunaā€¢15mo ago
not really again
addamsson
addamssonOPā€¢15mo ago
i dno't have this data on my server
treble/luna
treble/lunaā€¢15mo ago
all guilds etc are cached upon joining a guild, the cache gets updated
addamsson
addamssonOPā€¢15mo ago
how does the data get into my server? certainly not by teleporting
Unknown User
Unknown Userā€¢15mo ago
Message Not Public
Sign In & Join Server To View
addamsson
addamssonOPā€¢15mo ago
I don't know what aa "Guilds intent" is. I've never used anything else apart from discord.js
d.js docs
d.js docsā€¢15mo ago
guide Popular Topics: Gateway Intents read more
Unknown User
Unknown Userā€¢15mo ago
Message Not Public
Sign In & Join Server To View
addamsson
addamssonOPā€¢15mo ago
ah, that's what you mean so this means that whenever I create the Client it will download all guilds / channels / roles? isn't that a lot of data?
treble/luna
treble/lunaā€¢15mo ago
the guilds intent also is required for basic functions
addamsson
addamssonOPā€¢15mo ago
i mean how many guilds are there globally?
treble/luna
treble/lunaā€¢15mo ago
depends on how many guilds your bot is in
addamsson
addamssonOPā€¢15mo ago
oh isee damn it only loads the guilds where my bot is present lol makes sense so if i have this intent i can do the .cache.whatever approach i don't need to fetch
treble/luna
treble/lunaā€¢15mo ago
for guilds, channels and roles yes
addamsson
addamssonOPā€¢15mo ago
but will fetch throw ? if the guild is not there
treble/luna
treble/lunaā€¢15mo ago
the promise will reject and getting from cache would return undefined
addamsson
addamssonOPā€¢15mo ago
so if I await then it will throw
treble/luna
treble/lunaā€¢15mo ago
if you fetch yes
addamsson
addamssonOPā€¢15mo ago
nice, thanks
treble/luna
treble/lunaā€¢15mo ago
if you get from cache no
addamsson
addamssonOPā€¢15mo ago
so why creating a channel is bad UX?
treble/luna
treble/lunaā€¢15mo ago
.
addamsson
addamssonOPā€¢15mo ago
why is that bad? that's the part i don't understand
treble/luna
treble/lunaā€¢15mo ago
because you're giving everyone access to that channel some guilds have send / view perms locked behind a role
addamsson
addamssonOPā€¢15mo ago
i get that but you can't really determine if something is bad UX if you don't know what the use case is unless you think that creating channels is universally bad btw in this particular case i don't set permissions because it is just some example code that i wrote during code exploration šŸ˜…
Unknown User
Unknown Userā€¢15mo ago
Message Not Public
Sign In & Join Server To View
addamsson
addamssonOPā€¢15mo ago
when you invite the bot you go through a wizard and in that wizard you choose a channel in which the bot will post things you know about this when you invite the bot so if you know that the bot will do that and you specifically choose the channel you will ban the bot you just invited? šŸ˜…
addamsson
addamssonOPā€¢15mo ago
TOS of what? Discord? this is the actual code BTW:
//* šŸ‘‡ safe to do as discord will cache all servers where our bot resides
const guild = client.guilds.cache.get(serverId);
if (!guild) {
throw new ServerNotFoundError(serverId);
}

//* šŸ“˜ It is possible to have multiple channels with the same name
//* (even next to each other) so we need to use find
const exists = guild.channels.cache.find((channel) => {
const parent = channel.parent;
let parentCheck = true;
//* šŸ“˜ we don't care about the category of an existing channel
//* if it is not set in the params
if (category) {
parentCheck = parent?.name === category;
}
return (
channel.name === name &&
channel.type === ChannelType.GuildText &&
parentCheck
);
});

if (!exists) {
await guild.channels.create({
name,
type: ChannelType.GuildText,
parent: category,
topic: description,
});
}
//* šŸ‘‡ safe to do as discord will cache all servers where our bot resides
const guild = client.guilds.cache.get(serverId);
if (!guild) {
throw new ServerNotFoundError(serverId);
}

//* šŸ“˜ It is possible to have multiple channels with the same name
//* (even next to each other) so we need to use find
const exists = guild.channels.cache.find((channel) => {
const parent = channel.parent;
let parentCheck = true;
//* šŸ“˜ we don't care about the category of an existing channel
//* if it is not set in the params
if (category) {
parentCheck = parent?.name === category;
}
return (
channel.name === name &&
channel.type === ChannelType.GuildText &&
parentCheck
);
});

if (!exists) {
await guild.channels.create({
name,
type: ChannelType.GuildText,
parent: category,
topic: description,
});
}
so a channel is only created if it does not exist there is no /setup command though so if you just invite the bot to your server (without using our wizard) then nothing will happen šŸ˜…
Want results from more Discord servers?
Add your server