【问题标题】:How do I get a Discord bot to read data from a specific range of a Google Sheet?如何让 Discord 机器人从 Google 表格的特定范围读取数据?
【发布时间】:2020-02-29 09:16:57
【问题描述】:

有没有办法让 Discord 机器人从 Google 表格中查找数据?

示例情况: 如果 Discord 机器人收到命令“!points Bob”,机器人会在 google 表格中查找“Bob”并返回 Bob 拥有的点数。

我会假设我会使用模块 google-spreadsheet 但我是新手。有没有人可以帮忙?

【问题讨论】:

    标签: node.js google-sheets discord discord.js google-sheets-api


    【解决方案1】:

    您必须创建您的 discord 机器人来解析这些命令并将它们传送到 API。

    要访问 API,您必须对其进行授权。

    请参阅Quick-start Guide 了解如何操作。

    在你的应用授权之后,接下来就是根据你的聊天输入调用方法了。

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      你可以试试这样的,我还没有测试过! 我为此使用了googleapis npm-package。

      /*
      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
                      }
                  }
              }
              );
          });
      }
      

      要使用此功能,您必须从 api 传递身份验证(查看googleapis 包的文档)和名称。该函数返回一个承诺,然后就可以了,就像我说的我还没有在不和谐机器人中测试过这个。

      checkPoints(auth,name).then(points =>{
          console.log("Points  of player " + name + ": " + points);
      });
      

      【讨论】:

        猜你喜欢
        • 2021-03-17
        • 2020-06-03
        • 2022-01-21
        • 1970-01-01
        • 1970-01-01
        • 2019-08-26
        • 2021-09-09
        • 2022-06-14
        • 2021-04-30
        相关资源
        最近更新 更多