使用js的Discord-bot

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

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();

回答(1)

2 years ago

我对nedb了解不多,但你似乎回答了自己的问题 .

在db中查询目标的所有结果 .

创建一个初始化为false的标志,迭代所有返回的记录,如果匹配日期条件,则将标志设置为true .

然后,如果该标志仍为false,则创建一个DB条目 .