【问题标题】:Discord-bot using js使用 js 的不和谐机器人
【发布时间】:2019-02-14 11:08:54
【问题描述】:

我正在为这个不和谐机器人使用 nedb。我需要在特定日期完成该功能,它应该下载并保存作为我目标的人的所有对话。对于所有消息,我需要它来检查是否已存在于数据库中且已编辑日期小于。如果不存在,则插入。 有没有人可以帮助我?我真的很想弄清楚这一点

const fetch = require('node-fetch');
const moment = require('moment');
const config = require('./config');
const querystring = require("querystring");
var Datastore = require('nedb');

var db = new Datastore({ filename: 'discord.db', autoload: true });
const TO_DAYS      = 4194304 * 1000 * 60 * 60 * 24;
const ROOT_DATE    = moment([2018, 3, 30]);
const ROOT_DATE_ID = 440557948108800000;

// this inserts scott into the database
// var scott = {
//   name: 'Scott',
//   twitter: '@ScottWRobinson'
// };
// db.insert(scott, function (err, doc) {
//   console.log('Inserted', doc.name, 'with ID', doc._id);
// });


// this finds all items in the database and prints them.  
// db.find({}).exec(function (err, docs) {
//     console.log(docs);
//     docs.forEach(function (d) {
//         console.log('Found user:', d.name);
//     });
// });


const DATE_ID = function (date) {
    return ROOT_DATE_ID + date.diff(ROOT_DATE, "days") * TO_DAYS;
}

class discordApi {
  constructor() {}
  // this function sends a request to discords servers, and pulls down JSON
  get(momentDate, authorId, offset = 0) {
    const url =
      config.endpoint +
      querystring.stringify({
        min_id: DATE_ID(momentDate),
        author_id: authorId
      });
    return fetch(url, {
      headers: {
        method: "GET",
        Authorization: config.auth
      }
    }).then(res => {
      console.log(res.url);
      return res.json();
    });
  }
  // TODO finish this function.  for a specific date, it should download and save all the conversations for a person.
  // for all messages, check if already exist in database with edited_date less than.
  // if any do not exist, then insert.
  sync(momentDate) {
    // for this date, get all the messages from targets.
    config.targets.map(author_id => {
      this.get(momentDate, author_id).then(({ total_results, messages }) => {
        console.log(messages);
        messages.map(conversation => {
          conversation.map(({ attachments, author, content, timestamp }) => {
            console.log(timestamp, author.username, content);
          });
        });
      });
    });
  }
}

module.exports = new discordApi();

【问题讨论】:

    标签: javascript api bots discord nedb


    【解决方案1】:

    我对 nedb 了解不多,但您似乎大多回答了自己的问题。

    查询数据库以获取目标的所有结果。

    创建一个初始化为false的标志,遍历所有返回的记录,如果有匹配的日期条件,设置标志为true。

    如果标志仍然为假,则创建一个 DB 条目。

    【讨论】:

      猜你喜欢
      • 2021-09-12
      • 2018-03-20
      • 2019-08-03
      • 2021-05-24
      • 2021-02-12
      • 2021-08-14
      • 2018-11-10
      • 2021-12-21
      • 2020-07-03
      相关资源
      最近更新 更多