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:
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
- 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!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ā¢15mo ago
Message Not Public
Sign In & Join Server To View
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ā¢15mo ago
Message Not Public
Sign In & Join Server To View
why would you want to create a new channel? just make a command like /help or /setup
plus the channel isnt perm guarded
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:
I just noted that ?
is not required here as the signature doesn't have undefined
as a possible return valuesomething else than creating a channel
no need to fetch, all guilds are cached by the Guilds intent
why is creating a channel bad ux? you don't even know the use case š
is it a public bot?
because you're not setting perms for the channel
thus allowing everyone to see and send messages to it
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
all guilds, channels and roles are cached by the Guilds intent
Unknown Userā¢15mo ago
Message Not Public
Sign In & Join Server To View
for members you would need to fetch
what does "cached by the Guilds intent" mean?
exactly as says
also there needs to be a fetch operation at some point
not really
again
i dno't have this data on my server
all guilds etc are cached
upon joining a guild, the cache gets updated
how does the data get into my server? certainly not by teleporting
Unknown Userā¢15mo ago
Message Not Public
Sign In & Join Server To View
I don't know what aa "Guilds intent" is. I've never used anything else apart from discord.js
Unknown Userā¢15mo ago
Message Not Public
Sign In & Join Server To View
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?the guilds intent also is required for basic functions
i mean how many guilds are there globally?
depends on how many guilds your bot is in
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 fetchfor guilds, channels and roles yes
but will
fetch
throw ?
if the guild is not therethe promise will reject
and getting from cache would return undefined
so if I
await
then it will throwif you fetch yes
nice, thanks
if you get from cache no
so why creating a channel is bad UX?
.
why is that bad?
that's the part i don't understand
because you're giving everyone access to that channel
some guilds have send / view perms locked behind a role
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ā¢15mo ago
Message Not Public
Sign In & Join Server To View
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? š
TOS of what? Discord?
this is the actual code BTW:
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 š