如何存储消息内容

时间:2019-01-22 09:40:47

标签: mongodb mongoose discord.js

到目前为止,我已经能够将消息打印到控制台。数小时以来,我一直在寻找解决方案。我想做的是使用'!archive'+ message_id通过ID获取消息。然后,我要获取内容并将其存储到Mongo数据库中。也许我的模式是错误的,或者我尝试存储它的方式是错误的。到目前为止,这里是我的机器人代码的内容:

const discord = require('discord.js');
const bot = new discord.Client();
const config = require('./config.json');
const mongoose = require('mongoose');

let Msg = require('../models/messages');

function archiver() {

bot.on('ready', () => {
console.log(`Bot has started, with ${bot.users.size} users, in 
${bot.channels.size} channels`);
});

bot.on('message', (message) => {
    const channel = bot.channels.get('537131544821628929');
    const args = message.content.slice(config.prefix.length).split(' ');
    const command = args.shift().toLowerCase();
    let msg = new Msg(message.body);

    if(command === 'archive') {
        channel.fetchMessage(args[0])
        msg.save()
            .then(message =>  {
                Promise.resolve(message)
                console.log(message.content);
            })
            .catch(console.error);


        message.reply('Message successfully archived');
    }
});

数据库架构:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

 let Msg = new Schema({
     message: String
});

module.exports = mongoose.model('Msg', Msg);

使用此代码运行命令时,我得到undefined并将其保存为mongo db中一个带有id的空对象

0 个答案:

没有答案