🗣️Channels

Prototypes from ChannelManager

Getting a channel by ID

// discord.js
<Client>.channels.cache.get(string);

// djs-protofy
<Client>.channels.getById(string); // Channel | undefined
// OR
<Client>.channels.getById(string, ChannelType); // Channel | undefined

Getting a channel by Name

// discord.js
<Client>.channels.cache.find(channel => channel.name === string);

// djs-protofy
<Client>.channels.getByName(string | RegExp); // Channel | undefined
// OR
<Client>.channels.getByName(string | RegExp, ChannelType); // Channel | undefined

Getting a channel by Topic

// discord.js
<Client>.channels.cache.find(channel => channel.topic === string);

// djs-protofy
<Client>.channels.getByTopic(string | RegExp); // Channel | undefined
// OR
<Client>.channels.getByTopic(string | RegExp, ChannelType); // Channel | undefined

Getting a channel by Url

// discord.js
<Client>.channels.cache.find(channel => channel.url === "https://discord.com/...url");

// djs-protofy
<Client>.channels.getByUrl("https://discord.com/...url"); // Channel | undefined
// OR
<Client>.channels.getByUrl("https://discord.com/...url", ChannelType); // Channel | undefined

Getting a category by ID

// discord.js
<Client>.channels.cache.get(string);

// djs-protofy
<Client>.channels.getCategoryById(string); // CategoryChannel | undefined

Getting a category by Name

// discord.js
<Client>.channels.cache.find(channel => channel.name === string);

// djs-protofy
<Client>.channels.getCategoryByName(string | RegExp); // CategoryChannel | undefined

Getting a channel in another Shard with ID

// discord.js
await <Client>.shard.broadcastEval((shard) =>
  shard.channels.cache.get(string))
  .then(res => res.find(Boolean))
  .catch(error => console.log(error));

// djs-protofy
await <Client>.channels.getInShardsById(string); // Promise<Channel | null>
// OR
await <Client>.channels.getInShardsById(string, boolean); // Promise<APIChannel | Channel | null>

Getting a channel in another Shard with Name

// discord.js
await <Client>.shard.broadcastEval((shard) =>
  shard.channels.cache.find(channel => channel.name === string))
  .then(res => res.find(Boolean))
  .catch(error => console.log(error));

// djs-protofy
await <Client>.channels.getInShardsByName(string | RegExp); // Promise<Channel | null>
// OR
await <Client>.channels.getInShardsByName(string | RegExp, boolean); // Promise<APIChannel | Channel | null>

Getting a channel voice by an User ID

// discord.js
<Client>.channels.cache.find((channel) => {
  if (!channel.isVoiceBased()) return false;
  return channel.members.has(string);
})

// djs-protofy
<Client>.channels.getVoiceByUserId(string); // VoiceBasedChannel | undefined

Filtering channels by Types

// discord.js
<Client>.channels.cache.filter(channel => channel.type === ChannelType)

// djs-protofy
<Client>.channels.filterByTypes(ChannelType | ChannelType[]); // Collection<string, Channel>

Sending message through Shards easier

// discord.js
await <Client>.shard.broadcastEval(async (shard, { channelId, payload }) => {
  const channel = shard.channels.cache.get(channelId);
  if (!channel?.isTextBased()) return;
  return await channel.send(payload);
}, {
  context: {
    channelId,
    payload: {
      content: "...string",
      embeds: [],
      components: []
    }
  }
})
  .catch(console.error);

// djs.protofy
await <Client>.channels.send(channelId, payload);
/**
 * Promise<{
 *  error?: Error
 *  message?: Message
 *  success: boolean
 * }>
 */

Searching for a Channel

// discord.js
<Client>.channels.cache.find(channel => {
  return channel.name === string
    || channel.topic === string
    || channel.id === string
});

// djs-protofy
// string can be the channel ID, topic and name
<Client>.channels.searchBy(string); // Channel | undefined

// string[] can be multiple channels IDs, topics and names
<Client>.channels.searchBy(string[]); // Collection<string, Channel>

If you use an array of String, you will receive a Collection instead of Channel

Last updated