如何为Discord.js中添加,删除和更改的角色添加审核日志?

时间:2019-06-02 21:47:33

标签: discord.js

对于Discord.js机器人,我试图创建一个审核日志,该审核日志将消息发送到它所在的每个服务器中的特定日志通道,以供用户更改。我有一个有效的“已删除邮件”审核日志功能,但是当我尝试将其转移到角色添加和删除,用户名,昵称和头像更改时,该机器人将无法记录该日志并崩溃。如何在我的代码中解决此问题?

我同时包含了消息删除审核日志消息发送和角色添加/删除/用户名更改

client.on('messageDelete', function (message) {
  if (message.channel.type === 'text') {
    // post in the server's log channel, by finding the accuratebotlog channel (SERVER ADMINS **MUST** CREATE THIS CHANNEL ON THEIR OWN, IF THEY WANT A LOG)
    var log = message.guild.channels.find('name', CHANNEL)
    if (log != null) {
      log.sendMessage('**Message Deleted** ' + message.author + '\'s message: ' + message.cleanContent + ' has been deleted.')
    }
  }
})

// sends message when important (externally editable) user statuses change (for example nickname)
// user in a guild has been updated
client.on('guildMemberUpdate', function (guild, oldMember, newMember) {
  // declare changes
  var Changes = {
    unknown: 0,
    addedRole: 1,
    removedRole: 2,
    username: 3,
    nickname: 4,
    avatar: 5
  }
  var change = Changes.unknown

  // check if roles were removed
  var removedRole = ''
  oldMember.roles.every(function (value) {
    if (newMember.roles.find('id', value.id) == null) {
      change = Changes.removedRole
      removedRole = value.name
    }
  })

  // check if roles were added
  var addedRole = ''
  newMember.roles.every(function (value) {
    if (oldMember.roles.find('id', value.id) == null) {
      change = Changes.addedRole
      addedRole = value.name
    }
  })

  // check if username changed
  if (newMember.user.username != oldMember.user.username) {
    change = Changes.username
  }
  // check if nickname changed
  if (newMember.nickname != oldMember.nickname) {
    change = Changes.nickname
  }
  // check if avatar changed
  if (newMember.user.avatarURL != oldMember.user.avatarURL) {
    change = Changes.avatar
  }
  // post in the guild's log channel
  var log = guild.channels.find('name', CHANNEL)
  if (log != null) {
    switch (change) {
      case Changes.unknown:
        log.sendMessage('**[User Update]** ' + newMember)
        break
      case Changes.addedRole:
        log.sendMessage('**[User Role Added]** ' + newMember + ': ' + addedRole)
        break
      case Changes.removedRole:
        log.sendMessage('**[User Role Removed]** ' + newMember + ': ' + removedRole)
        break
      case Changes.username:
        log.sendMessage('**[User Username Changed]** ' + newMember + ': Username changed from ' +
          oldMember.user.username + '#' + oldMember.user.discriminator + ' to ' +
          newMember.user.username + '#' + newMember.user.discriminator)
        break
      case Changes.nickname:
        log.sendMessage('**[User Nickname Changed]** ' + newMember + ': ' +
          (oldMember.nickname != null ? 'Changed nickname from ' + oldMember.nickname +
            +newMember.nickname : 'Set nickname') + ' to ' +
          (newMember.nickname != null ? newMember.nickname + '.' : 'original username.'))
        break
      case Changes.avatar:
        log.sendMessage('**[User Avatar Changed]** ' + newMember)
        break
    }
  }
})

我希望漫游器向我的频道发送一条消息,说“用户角色已删除:memberName +他们的旧角色”,反之亦然,以添加角色,但是我的漫游器无法将这些消息发送到我设置的漫游器日志通道上。

1 个答案:

答案 0 :(得分:0)

guild事件中没有GuildMemberUpdate参数。因此,newMember是未定义的,因为仅传递了两个参数。

client.on('guildMemberUpdate', (oldMember, newMember) => {
  const guild = newMember.guild;

  // continue with code
});