“ collection#find:改为传递函数,Tags.create不是函数”的原因

时间:2019-06-10 03:55:20

标签: node.js sqlite sequelize.js discord.js

我正在设置Discord机器人,并且正在发出警告命令,其中所有条目都将记录在Sequelize表的内部。当前,我还使用主index.js,并具有一个包含所有命令的文件夹。机器人启动时,index.js会调用每个命令。我正在使用https://discordjs.guide/寻求帮助和指导。我使用了他们的Seqeulize数据库帮助,所以我在“添加标签”,我输入了他们所说的内容,除了我自己的列名。

我没有尝试任何事情,因为我不知道为什么会发生这种情况。我只是从JS开始并且是自学成才的,所以我并不了解。

const Tags = sequelize.define('moderation', {
  username: {
    type: Sequelize.STRING,
    unique: false,
  },
  case_num: {
    type: Sequelize.INTEGER,
    unique: true,
  },
  type: Sequelize.STRING,
  reason: Sequelize.STRING,
  length: Sequelize.STRING,
  by: { 
    type: Sequelize.STRING,
    unique: false,
  },
  date: {
    type: Sequelize.DATE,
  }
});
const Discord = require("discord.js");
const {parseUser} = require('../util/parseUser.js');
const {caseNumber} = require('../util/caseNumber.js');
const Sequelize = require("sequelize");
const Tags = require ('../modTable.js');

module.exports.run = async (bot, message, args) => {
  const sequelize = new Sequelize('database', 'user', 'password', {
    host: 'localhost',
    dialect: 'sqlite',
    logging: false,
    operatorsAliases: false,
    // SQLite only
    storage: 'database.sqlite',
  });
    if(!message.member.hasPermission(['KICK_MEMBERS'])) 
      return message.reply("Sorry, you don't have permissions to use this!");


    const user = message.mentions.users.first();
    if (user === message.author) {
        return message.channel.send('You cannot do that to yourself, why did you try?');
    }
    const modlogs = bot.channels.find('name', 'mod-logs');
    const caseNum = await caseNumber(bot, modlogs);
    if (!modlogs) return message.reply('I cannot find a mod-log channel');
    if (message.mentions.users.size < 1) return message.reply('You must mention someone to warn them.').catch(console.error);
    const reason = args.splice(1, args.length).join(' ') || `no reason provided`;
    let date = new Date();
    const type_warn = "warn";

    try {
      const tag = await Tags.create({
        username: message.mentions.users.first(),
        case_num: caseNum,
        type: type_warn,
        reason: reason,
        length: null,
        by: message.author.username,
        date: date,
      });
      console.log(e);
    }
    catch (e) {
      console.log(e);
      return message.reply('Something went wrong with adding this warn to the table. Please contact Charlee or VorTex eXDee!');
    }

我希望事情会被记录下来,并且终端上不会出现错误,但是它说:

  

Deprecationwarning: collection#find: pass a function instead

     

TypeError: Tags.create is not a function.

1 个答案:

答案 0 :(得分:0)

  

Collection#find: pass a function instead

Collection.find()可以使用函数而不是属性和值。不建议使用后者。

解决方案:

Collection.find(element => element.property === value)

(您也可以通过其他方式比较属性,一次比较多个属性。)


  

Tags.create() is not a function

当您需要modTable.js时,您就将Tags声明为文件的module.exports属性。您可以为module.exports分配值并为其创建属性,以便可以在其他位置访问变量。

解决方案:

从文件中导出变量。

module.exports = Tags;
// You can also use: module.exports.Tags = Tags;