简体   繁体   中英

discord.js check if guild has a channel with a specific name, and if so, store that channels id to a variable

I am using discord.js. Is there a way to check if a guild has a channel with a specific name, and if so, store that channels id to a variable? I am trying to make a command that logs it's actions to a channel with the name 'logs' if it exists.

A Guild has a channels property which returns a Collection of GuildChannels . Using Collection.find() , you can search for the channel by name by comparing Channel.name in the predicate function. If a channel is found, you can read its id property to retrieve its Snowflake ID.

For example...

const channel = /* Guild */.channels.find(c => c.name === 'some-name');
const id = channel ? channel.id : null;

A Guild has a Collection of channels . A Collection has a .find(propOrFn) method that lets you find an item based on one of their properties, in this example the .name of a GuildChannel .

Untested, but this should work:

const channel = guild.channels.find(chan => chan.name === 'YOUR_NAME');
// channel.id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM