R
R
rodgi2019-07-24 18:29:16
JavaScript
rodgi, 2019-07-24 18:29:16

(Discord.js) Why doesn't .setColor work?

".setColor" doesn't work, throws an error: "Cannot read property 'setColor' of null". Vod code:

const Discord = require('discord.js');
const client = new Discord.Client();
const config = require('./config.json');

const size    = config.colors;
const rainbow = new Array(size);

for (var i=0; i<size; i++) {
  var red   = sin_to_hex(i, 0 * Math.PI * 2/3); // 0   deg
  var blue  = sin_to_hex(i, 1 * Math.PI * 2/3); // 120 deg
  var green = sin_to_hex(i, 2 * Math.PI * 2/3); // 240 deg

  rainbow[i] = '#'+ red + green + blue;
}

function sin_to_hex(i, phase) {
  var sin = Math.sin(Math.PI / size * 2 * i + phase);
  var int = Math.floor(sin * 127) + 128;
  var hex = int.toString(16);

  return hex.length === 1 ? '0'+hex : hex;
}

let place = 0;
const servers = config.servers;

function changeColor() {
  for (let index = 0; index < servers.length; ++index) {		
    client.guilds.get(servers[index]).roles.find('name', config.roleName).setColor(rainbow[place])
    .catch(console.error);
    
    if(config.logging){
      console.log(`Изменяется ${rainbow[place]} на сервере: ${servers[index]}`);
    }
    if(place == (size - 1)){
      place = 0;
    }else{
      place++;
    }
  }
}

client.on('ready', () => {
  console.log(`Логин ${client.user.username}!`);
  setInterval(changeColor, config.speed);
});


client.login(config.token);

Error on line 29, 74 characters.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zage, 2019-08-07
@rodgi

For this script to work, you need to make a small change to the 'ChangeColor' function.
I'll write you the full function right away, you just need to insert it.

function changeColor() {
  for (let index = 0; index < servers.length; ++index) {		
    bot.guilds.get(servers[index]).roles.find(role => role.name === "RoleName").setColor(rainbow[place])
    .catch(console.error);
    
    if(rbconf.logging){
      console.log(`[ColorChanger] Changed color to ${rainbow[place]} in server: ${servers[index]}`);
    }
    if(place == (size - 1)){
      place = 0;
    }else{
      place++;
    }
  }
}

Instead of RoleName , you insert the name of the role for which you would like to change the color.
Good luck!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question