使用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