Reputation: 88
Is there a way to get a discord bot to lookup data from a Google sheet?
Example Situation: If the Discord bot is presented with a command "!points Bob", the bot would lookup the google sheet for "Bob" and return the number of points that Bob has.
I would assume that I would use the module google-spreadsheet but I am new. Is there anyone that can help out?
Upvotes: 1
Views: 8579
Reputation: 303
You can try something like this, i havnt tested this yet!
I used the googleapis
npm-package for this.
/*
Assuming your table is something like this:
|------------------|
| Player | Points |
|------------------|
| Player1 | 0 |
| Player2 | 100 |
|------------------|
*/
function checkPoints(auth,name) {
return new Promise((resolve, reject) => {
const sheets = google.sheets({ version: 'v4', auth });
sheets.spreadsheets.values.get({
spreadsheetId: '<your spreadsheetid>',
range: '<Your Points sheetname>!A2:B', // A2 because i assume you got a title like "Name/Username" etc.
}, (err, res) => {
if (err) return console.log("The API returned an error: " + err);
let list = [];
for(let i = 0; i < res[0].length; i++){ //Loop throw all Players
if(res[0][i] == name){
resolve(res[1][i]); //Returns the Points of the Player
}
}
}
);
});
}
To use this function you must pass the auth from the api (take a look at the documentation of the googleapis
Package) and a name. The function returns a promise, and there you go, like i said i havent tested this within a discord bot yet.
checkPoints(auth,name).then(points =>{
console.log("Points of player " + name + ": " + points);
});
Upvotes: 1
Reputation: 2770
You would have to create your discord bot to parse these commands and pipe them to the API.
To access the API, you have to authorize it.
Refer to the Quick-start Guide to learn how to do so.
After you have your application authorized, then it's all a matter of calling the method based on your chat input.
Hope this helps!
Upvotes: 0