随机选择压制

时间:2017-06-19 21:14:38

标签: javascript postgresql ecmascript-6 discord

使用Squelize我想在这个模型中找到一个随机标签:

 const Sequelize = require('sequelize');

const Database = require('../PostgreSQL');


let Tag = Database.db.define('tags', {
    userID: { type: Sequelize.STRING },
    userName: { type: Sequelize.STRING },
    guildID: { type: Sequelize.STRING },
    guildName: { type: Sequelize.STRING },
    name: { type: Sequelize.STRING },
    content: { type: Sequelize.STRING(1800) }, // eslint-disable-line new-cap
    type: {
        type: Sequelize.BOOLEAN,
        defaultValue: false
    },
    example: {
        type: Sequelize.BOOLEAN,
        defaultValue: false
    },
    exampleID: {
        type: Sequelize.STRING,
        defaultValue: ''
    },
    uses: {
        type: Sequelize.INTEGER,
        defaultValue: 0
    }
});


module.exports = Tag;

这是我的实际代码,我正在使用discord.js。所以我尝试在随机标签中挑选所有标签。

    async run(msg) {
        const tag = Tag.findAll({ pickRandomProperty, where: { guildID: msg.guild.id } });
        if (!tag) return null;

        console.log(tag.content)
        return msg.say(tag.content);
    }
};





function pickRandomProperty(obj) {
    var result;
    var count = 0;
    for (var prop in obj)
        if (Math.random() < 1/++count)
           result = prop;
    return result;
}

看起来那样,因为用过w / discord.js

0 个答案:

没有答案