Discord v14 AutoRole Bot, Anyone like to help me out?

A Discord Bot the adds roles to members on current activity / Game being played

const { Client, GatewayIntentBits } = require('discord.js')
const config = require('./config.json')

const prefix = config.prefix // BOT PREFIX

const roleGame = {
  'A Way Out': {
    name: 'a way out',
    id: '760652119119364137'
  },
  'VALORANT': {
    name: 'valorant',
    id: '760583117940457494'
  }

}

const blackListUsers = ['297153970613387264', '240482527695994880', '168331481024823296', '1081085914928906280'] // OTHER BOTS
const blackListWords = ['spotify', 'custom', 'status', 'noxplayer'] // BLACKLIST ROLES UPDATE

// intents
const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMembers,
    GatewayIntentBits.GuildPresences,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent
  ]
})

// Collects Role Ids

function collectRoleIds () {
  client.guilds.cache.forEach(function (server) {
    server.roles.cache.forEach(function (role) {
      for (const gKey in roleGame) {
        if (prefix + gKey === role.name) {
          roleGame[gKey].id = role.id
        }
      }
    })
  })
}

function registeredGame (game) {
  game.toString()
  for (const gKey in roleGame) {
    if ((game !== null && game !== undefined) && roleGame[gKey].name === game.toString().toLowerCase()) {
      return gKey
    }
  }

  return false
}

function removeWords (text) {
  let str2 = text.replace(/,/g, ' ')
  const str3 = str2.toLowerCase()
  str2 = str3.split(' ')

  for (let i = 0; i < blackListWords.length; i++) {
    for (let j = 0; j < str2.length; j++) {
      if (str2[j].toLowerCase() === blackListWords[i]) {
        str2.splice(j, 1)
      }
    }
  }
  return text = str2.join(' ')
}

Error occurs here on the final line .activities

client.on('presenceUpdate', (oldPresence, newPresence) => {
  // if (JSON.stringify(oldPresence) == 'undefined') {return};

  if (JSON.stringify(oldPresence) == undefined) {
    return
  }
  if (oldPresence == 'undefined') {
    return
  }
  if (oldPresence == null && newPresence == null) {
    return
  }
  if (oldPresence == 'undefined' && newPresence == 'undefined') {
    return
  }
  if (oldPresence.activities == 'undefined' && newPresence.activities == 'undefined') {
    return
  }
 // Removes Roll on disconnect and adds roll on reconnect
  const Role = newPresence.guild.roles.cache.get("274906098534711297"); // Finding the required role.
    if (!Role) {return console.error("No role found.")}; // If the role doesn't exist we return an error.

    if (newPresence.status == "offline") { // online - user is online; idle - user is AFK; offline - user is offline or invisible; dnd - user is in Do Not Disturb;
      newPresence.member.roles.remove(Role).catch(e => {console.error(e)}); // Removing the role since the user is offline.
        } else {
      newPresence.member.roles.add(Role).catch(e => {console.error(e)}); // Adding the role since the user is Online/Idle/Do Not Disturb
  }

Next error appears here on userID

 // REMOVE BOTS UPDATE
  for (const ii in blackListUsers) {
    if (oldPresence.userId.includes(blackListUsers[ii].toLowerCase())) {
      return
    }
  } 
  var oldActivities = oldPresence.activities.toString()
  var newActivities = newPresence.activities.toString()

  // REMOVE WORDS
  var oldActivities = removeWords(oldActivities)
  var newActivities = removeWords(newActivities)

  if (oldActivities === newActivities) {
    return
  }

  console.log('\n########################################################################################################################\n')
  console.log('old activity: ' + oldActivities + '\nNew activity: ' + newActivities + '\nUser: ' + newPresence.member.displayName)

  const oldGame = registeredGame(oldActivities)
  if (oldGame) {
    newPresence.member.roles.remove(roleGame[oldGame].id).catch(e => {
      console.error(e)
    })
    console.log('Remove Role')
  }

  const gameId = registeredGame(newActivities)
  if (gameId) {
    newPresence.member.roles.add(roleGame[gameId].id).catch(e => {
      console.error(e)
    })
    console.log('Add Role')
  }
})

client.on('ready', () => {
  console.log('Bot serving. Setting status...')
  client.user.setStatus('online')

  collectRoleIds()
})

client.login(config.token)

Uncaught TypeError TypeError: Cannot read properties of null (reading 'activities')

can you send the docs for this event? maybe they’ve changed it not to have the oldPresence or newPresence parameters?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.