简体   繁体   中英

Get Discord Bot to read and update spreadsheet in JavaScript via Google Sheets API

I have a Discord bot that I host on glitch.com whose aim is to store, read and update some basic stats for the users. Since this setup caused the variables to reset each time the project went to sleep, I successfully setup the Google Sheets API so that I could store the user's stats somewhere else permanently. However, I cannot for the love of God figure out how to actually make my JavaScript variables get their value from the spreadsheet.

My initial code simply had a lot of variables that stored each member's various stats, for example:

var User1Gold = 0 
var User2Gold = 0

And so on. This was very easy to work with for a beginner hobbyist like me, but now I find myself reading the documentation over and over (I suspect the answer I seek is here ) and getting nothing in the clear. Is there a way to make my variables equal a certain cell of a certain spreadsheet and just be done with it, or is it nowhere near as easy as that? Perhaps something like this:

var User1Gold = {spreadsheetId: 'iD', range: 'B3',};

Edit

Okay! I moved forward! I was able to make my bot access the spreadsheet to find a specific value and then send a message with that value. Just what I wanted, except that it seems to be only able to do it once? This is the example code that came with the API that I tweaked to make my variable equal one cell from my spreadsheet.

var myvariable

function getvalue(auth) {
  const sheets = google.sheets({version: 'v4', auth});
  sheets.spreadsheets.values.get({
    spreadsheetId: 'SpreadsheetId',
    range: 'Sheet1!B3',
  }, (err, res) => {
    if (err) return console.log('The API returned an error: ' + err);
    const rows = res.data.values;
    if (rows.length) {
      rows.map((row) => {
      myvariable = (`${row[0]}`);
      });
    } else {
      console.log('No data found.');
    }
  });
} 

This works just as intended, but I obviously want to reference more than one value from my spreadsheet (user health points, coins, and so on). But when I copy-pasted the above a second time, giving the variable and the function another name, it just doesn't work. I get the error message:

(node:2930) UnhandledPromiseRejectionWarning: DiscordAPIError: Missing Permissions

What am I doing wrong? Why does my first function seemingly have permissions, but my second one which is identical does not? Is there an easier way to do this than declaring a function for each value I want to obtain?

Edit 2

I was able to fix the error (don't know how to be honest), but my problem persists. I cannot make a second function to retrieve a second value from my spreadsheet and assign it to a second variable. I get no errors in the console, but the program simply doesn't do it, while the first function (the one in the example) works just fine.

Okay, I managed to fix it, so I'm leaving this here in case someone else has the same issue. To make it work I simply had to call for all the values I needed in the original function (which returns an array), and then set each variable I wanted to a specific value inside the array.

var myvar1
var myvar2

    function getvalue(auth) {
      const sheets = google.sheets({version: 'v4', auth});
      sheets.spreadsheets.values.get({
        spreadsheetId: 'SpreadsheetId';
        range: 'Copa!B2:B3',
      },(err, res) => {
        if (err) return console.log('The API returned an error: ' + err);
        myvar1 = res.data.values[0];
        myvar2 = res.data.values[1];
        });
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM