Reputation: 55
I want to create a js function that takes discord serverId in parameter fetch and return array contains all channels Id in that server
Upvotes: 5
Views: 14929
Reputation: 1319
For discord.js v13 you could try the following.
const discordServer = client.guilds.cache.get(DISCORD_SERVER_ID);
const channels = discordServer?.channels ? JSON.parse(
JSON.stringify(discordServer.channels)
).guild.channels : [];
It will return you the list of channel ids.
Upvotes: 2
Reputation: 17
If you still interested in this for V12
var array = [];
function getChannelIDs(fetch)
{
try{
let channels = client.channels.cache.array();
for (const channel of channels)
{
array.push(channel.id);
console.log(channel.id);
}}catch(err){
console.log('array error')
message.channel.send('An error occoured while getting the channels.')
console.log(err)
}
return array;
}
getChannelIDs()
This fixes the problem with not getting the array correctly
Upvotes: 0
Reputation: 2145
Since this isn't a problem, I'll give you code and explain.
function getChannelIDs(fetch)
{
var array = [];
let channels = client.guilds.channels;
for (const channel of channels.values())
{
array.push(channel.id);
console.log(channel.id);
}
return array;
}
First, it creates an array called array
. After that, it gets the channels from the guild. Next, for every channel, it pushes it's ID to the array and logs it. Finally, it returns the array.
Upvotes: 4