如何通过guildMemberAdd事件设置频道名称?

时间:2020-09-20 18:44:23

标签: discord discord.js

我正在尝试创建一个语音通道,以显示服务器上的成员数量,但是我似乎无法获得更改该通道的ID。我在guildMemberAdd事件中有以下代码:

bot.on('guildMemberAdd', member => {
  const channel = member.guild.channels.cache.find(ch => ch.name === 'factions');
  channel.send(`Welcome to the server, <@${member.id}>! Make sure to check out <#757221255198802051> and <#757221255198802051>!`);
  
  const serverCount = (bot.guilds.cache.get(member.guild.id).memberCount);
  member.guild.channels.find(ch => ch.name === "Server Count:").setName(`Server Count: ${serverCount}`);
});

由于member.guild.channels.find is not a function,所有这些都与最后一行分开。我知道我需要做channel.setName,但是我不确定如何获取通道来设置传递给member事件的guildMemberAdd对象的名称。

1 个答案:

答案 0 :(得分:1)

您忘记了cachestartsWith()这两个功能。要解决您的问题,

bot.on('guildMemberAdd', member => {
  const channel = member.guild.channels.cache.find(ch => ch.name === 'factions');
  channel.send(`Welcome to the server, <@${member.id}>! Make sure to check out <#757221255198802051> and <#757221255198802051>!`);
  
  const serverCount = member.guild.memberCount;
  member.guild.channels.cache.find(ch => ch.name.startsWith("Server Count: ")).setName(`Server Count: ${serverCount}`);
});