discord.js 检查公会是否有一个具有特定名称的频道,如果有,将该频道 ID 存储到一个变量中

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

我正在使用 discord.js。有没有办法检查一个公会是否有一个具有特定名称的频道,如果有,将该频道 ID 存储到一个变量中?我正在尝试创建一个命令,将其操作记录到名称为 'logs' 的频道(如果存在)。

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 属性 检索其 Snowflake ID。

例如...

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

一个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.

未经测试,但这应该有效:

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