Discord.js如何使用用户的id获取用户昵称

时间:2018-03-13 12:11:26

标签: javascript node.js discord discord.js

我正在使用client.on('voiceStateUpdate ...)来检查用户何时离开或加入了语音频道,并将用户的消息发送为“@< \ id>”这是他们的事。

ping使他们感到不安,我正试图找到一种方式,在这种情况下,它发送一个没有@的昵称信息(msg.replace('@','')不起作用。)

如果我已拥有用户的ID,我该怎么做才能获得用户的昵称?

我的代码现在看起来像这样:

client.on('voiceStateUpdate', (oldMember, newMember) => {
  let newUserChannel = newMember.voiceChannel;
  let oldUserChannel = oldMember.voiceChannel;
  var channel = client.channels.get('385344314362036224');
  if(oldUserChannel === undefined && newUserChannel !== undefined) {
    channel.sendMessage(newMember + ' has joined a voice channel');
  } else if(newUserChannel === undefined){
    channel.sendMessage(oldMember + ' has left a voice channel');
  }
});

1 个答案:

答案 0 :(得分:1)

oldMember和newMember属于" GuildMember"。您可以在此处找到使用Guild会员可以执行的所有操作:https://discord.js.org/#/docs/main/stable/class/GuildMember

因此,使用昵称的代码应如下所示:

client.on('voiceStateUpdate', (oldMember, newMember) => {
  let newUserChannel = newMember.voiceChannel;
  let oldUserChannel = oldMember.voiceChannel;
  var channel = client.channels.get('385344314362036224');
  if(oldUserChannel === undefined && newUserChannel !== undefined) {
    channel.sendMessage(newMember.displayName + ' has joined a voice channel');
  } else if(newUserChannel === undefined){
    channel.sendMessage(oldMember.displayName + ' has left a voice channel');
  }
});