Search code examples
javascriptnode.jsdiscorddiscord.js

Cannot put a variable inside channels.get() on discord.js


I've been trying to do some workaround on this thing but it just drives me crazy. The way I see it, it should work just fine but there is something going wrong with it... I hope I am not just really tired and missed a very easy mistake.

Anyway here is the thing. It's something pretty easy to do. an event is activated and then the algorithm below is supposed to read a certain database where I have the ids of the channels I want to send to stored, and it does one loop for each of those ids, outputting a different id on each loop,

(note: I am not doing it inside a client.on("message) event)

let channel = client.channels.get(dbresult)
channel.send(`test`);

This part was supposed to run 2 times with 2 separate numbers on the dbresult variable(since i have just 2 numbers on the database).

Now, when I run this part with just the id number alone and not the dbresult variable like this:

rows.forEach(function(row){
      //let dbresult = row.newsid.toString()
     let channel = client.channels.get(`0123456789`)
     channel.send(`test`);
   })

the script works just fine, but when I use the dbresult variable, it gives me this error

channel.send(`test`);
             ^

TypeError: Cannot read property 'send' of undefined

Now these are the things I've tried:

  • Putting a console.log(dbresult) just inside the foreach() block. It reads the ids just fine!
  • Making dbresult a string (as you can see) and also I've tried not converting it into a string too
  • Just Because I was desperate I even tried this: channel.send(${dbresult}), just in case.

Things that work:

  • The things I want to get triggered outside this part of the script are just fine, the script always gets activated successfully.
  • The script always reads each and every one of the ids of the channel. By removing the insides of "foreach" function and putting a console.log(dbresult), I saw that it outputs the different ids I want it to output.

Here is the whole script:

  let db = new sqlite.Database('./databases/Serverinfo', sqlite.OPEN_READWRITE | sqlite.OPEN_CREATE);
  const sql = 'SELECT newsid FROM news';
  db.all(sql, function(error,rows){
    if (rows.length <= 0){
      return;
    }
    if (error){
      throw error;
    }
    rows.forEach(function(row){
      let dbresult = row.newsid.toString()
      console.log(dbresult)
     let channel = client.channels.get(dbresult)
     channel.send(`test`);
   })
   db.close();
  })

Any help is appriciated of course


Solution

  • If you are using discord.js v.12 you need to use the cache collection.

    And yes, the id you want to use needs to be a string.

    let channel = client.channels.cache.get(dbresult);
    

    https://discord.js.org/#/docs/main/stable/class/ChannelManager

    What you might consider doing is checking if the channel was found before you try to send something.

    EDIT: The problem after updating comes from the fact that you store the channelID in your database as an INT which brings the following problem: Numeric literals with absolute values equal to 2^53 or greater are too large to be represented accurately as integers.

    What that means is that it doesn't store the ID correctly because it is too large, we are talking in the Quadrillions here. This could happen either at your database or when you import the number into your code. Without knowing your DB setup I can't tell.

    Example:

    683328323304292410 // the actual ID of the channel
    
    683328323304292400 // the INT that is stored
    

    To fix this you have two options and both need to be implemented in your database.

    • You can either convert the datatype for the row which stores the channelID to bigINT or convert it when you import the ID source

    • You can store the channelID as a string in the first place. (probably the easier solution)