From ed1f42452139bacb3c6e6b6baea48bc60a2cfae9 Mon Sep 17 00:00:00 2001 From: NukeninDark Date: Wed, 4 Aug 2021 03:22:27 +0000 Subject: [PATCH] Adicionado quiz e database --- commands/chifres/quiz.js | 137 +++++++++++++++++++++++++++++++++++++++ database/dbInitiate.js | 23 +++++++ database/dbObjects.js | 57 ++++++++++++++++ database/models/quiz.js | 0 database/models/users.js | 25 +++++++ 5 files changed, 242 insertions(+) create mode 100644 commands/chifres/quiz.js create mode 100644 database/dbInitiate.js create mode 100644 database/dbObjects.js create mode 100644 database/models/quiz.js create mode 100644 database/models/users.js diff --git a/commands/chifres/quiz.js b/commands/chifres/quiz.js new file mode 100644 index 0000000..9f43c7f --- /dev/null +++ b/commands/chifres/quiz.js @@ -0,0 +1,137 @@ +const Discord = require("discord.js"); +const { perguntas } = require("./configs/perguntasQuiz.json"); +const Sequelize = require("sequelize"); +const { Users } = require("../../database/dbObjects.js"); + +module.exports = { + name: "quiz", + description: `Descubra se você é mais Lana ou Julia.`, + execute(message, args) { + message.reply("Vai começar o quiz de Laninha e Julinha!"); + let perguntaAtual = 0, + laninhaPontos = 0, + julinhaPontos = 0; + this.perguntaQuiz(message, perguntaAtual, laninhaPontos, julinhaPontos); + }, + + async perguntaQuiz(message, perguntaAtual, laninhaPontos, julinhaPontos) { + const quizEmbed = new Discord.MessageEmbed() + .setColor("RANDOM") + .setTitle("Quiz da Gêmeas") + .setDescription("\u200B") + .setThumbnail(perguntas[perguntaAtual].thumbnail) + .setImage(perguntas[perguntaAtual].image) + .setTimestamp() + .setFooter( + message.author.username, + message.author.displayAvatarURL({ dynamic: true }) + ) + .addFields( + { + name: perguntas[perguntaAtual].pergunta, + value: "\u200b", + inline: false, + }, + //{ name: '\u200B', value: '\u200B', inline: false }, + { + name: "🇦", + value: `**${perguntas[perguntaAtual].A}**`, + inline: true, + }, + { name: "\u200B", value: "\u200B", inline: true }, + { + name: "🇧", + value: `**${perguntas[perguntaAtual].B}**`, + inline: true, + }, + { name: "\u200B", value: "\u200B", inline: false }, + { + name: "🇨", + value: `**${perguntas[perguntaAtual].C}**`, + inline: true, + }, + { name: "\u200B", value: "\u200B", inline: true }, + { + name: "🇩", + value: `**${perguntas[perguntaAtual].D}**`, + inline: true, + } + ); + + message.reply(quizEmbed).then( (sentMessage) => { + const emojis = ["🇦", "🇧", "🇨", "🇩"]; + emojis.forEach(async (emoji) => { + await sentMessage.react(emoji); + }); + + const filter = (reaction, user) => + (reaction.emoji.name === emojis[0] || + reaction.emoji.name === emojis[1] || + reaction.emoji.name === emojis[2] || + reaction.emoji.name === emojis[3]) && + user.id === message.author.id; + const collector = sentMessage.createReactionCollector(filter, { + time: 60000, + max: 1, + }); + collector.on("collect", async (r) => { + if ( + r.emoji.name === perguntas[perguntaAtual].Lana[0] || + r.emoji.name === perguntas[perguntaAtual].Lana[1] + ) { + message.reply(`Laninha ${r.emoji.name}`); + laninhaPontos++; + } else if ( + r.emoji.name === perguntas[perguntaAtual].Julia[0] || + r.emoji.name === perguntas[perguntaAtual].Julia[1] + ) { + message.reply(`Julinha ${r.emoji.name}`); + julinhaPontos++; + } + + if (perguntaAtual < perguntas.length - 1) { + this.perguntaQuiz( + message, + ++perguntaAtual, + laninhaPontos, + julinhaPontos + ); + } else { + if (laninhaPontos > julinhaPontos) { + laninhaPontos / perguntas.length < 0.6 + ? message.reply( + `Laninha are the champion! Lana: ${laninhaPontos}, Julia: ${julinhaPontos}, Porcentagem: ${ + laninhaPontos / perguntas.length + }` + ) + : message.reply( + `Laninha foi por pouco! Lana: ${laninhaPontos}, Julia: ${julinhaPontos}, Porcentagem: ${ + laninhaPontos / perguntas.length + }` + ); + const user = await Users.findOne({ + where: { user_id: message.author.id }, + }); + await user.lanaWinner() + } else { + julinhaPontos / perguntas.length < 0.6 + ? message.reply( + `Julinha are the champion! Lana: ${laninhaPontos}, Julia: ${julinhaPontos}, Porcentagem: ${ + julinhaPontos / perguntas.length + }` + ) + : message.reply( + `Julinha foi por pouco! Lana: ${laninhaPontos}, Julia: ${julinhaPontos}, Porcentagem: ${ + julinhaPontos / perguntas.length + }` + ); + const user = await Users.findOne({ + where: { user_id: message.author.id }, + }); + await user.increment('quizJulia'); + } + } + }); + }); + }, +}; diff --git a/database/dbInitiate.js b/database/dbInitiate.js new file mode 100644 index 0000000..3ec1523 --- /dev/null +++ b/database/dbInitiate.js @@ -0,0 +1,23 @@ +const Sequelize = require('sequelize'); + +const sequelizeGemeas = new Sequelize( + "database", + process.env.USERNAME, + process.env.PASSWORD, + { + host: "localhost", + dialect: "sqlite", + logging: false, + // SQLite only + storage: "./GemeasDB.sqlite", + } +); + +require('./models/Users')(sequelizeGemeas, Sequelize.DataTypes); + +const force = process.argv.includes('--force') || process.argv.includes('-f'); + +sequelizeGemeas.sync({ force }).then(async () => { + console.log('Database synced'); + sequelizeGemeas.close(); +}).catch(console.error); \ No newline at end of file diff --git a/database/dbObjects.js b/database/dbObjects.js new file mode 100644 index 0000000..52152e3 --- /dev/null +++ b/database/dbObjects.js @@ -0,0 +1,57 @@ +const Sequelize = require("sequelize"); + +const sequelizeGemeas = new Sequelize( + "GemeasDB", + process.env.USERNAME, + process.env.PASSWORD, + { + host: "localhost", + dialect: "sqlite", + logging: false, + // SQLite only + storage: "./database/GemeasDB.sqlite", + } +); + +const Users = require("./models/Users")(sequelizeGemeas, Sequelize.DataTypes); + +Users.prototype.addItem = async function(item) { + const useritem = await UserItems.findOne({ + where: { user_id: this.user_id, item_id: item.id }, + }); + + if (useritem) { + useritem.amount += 1; + return useritem.save(); + } + + return UserItems.create({ user_id: this.user_id, item_id: item.id, amount: 1 }); +}; + +Users.prototype.addWinner = async function(winner){ + const affectedRows = await Users.update({ quizUltimo: winner }, { where: { user_id: this.user_id } }); + if (affectedRows > 0) { + return message.reply(`Foi adicionado **${winner}** como vencerdor.`); + } + return message.reply(`Nao foi possivel adicionar **${winner}** como vencedora.`); +} + +Users.prototype.lanaWinner = async function(){ + const twinWinner = await Users.findOne( { where: { user_id: this.user_id } }); + + if (twinWinner) { + return twinWinner.increment('quizLana'); + } + return message.reply('Não foi possivel adicionar o vencedor Lana no Database.') +} + +Users.prototype.juliaWinner = async function(){ + const twinWinner = await Users.findOne( { where: { user_id: this.user_id } }); + + if (twinWinner) { + return twinWinner.increment('quizJulia'); + } + return message.reply('Não foi possivel adicionar o vencedor Julia no Database.') +} + +module.exports = { Users }; \ No newline at end of file diff --git a/database/models/quiz.js b/database/models/quiz.js new file mode 100644 index 0000000..e69de29 diff --git a/database/models/users.js b/database/models/users.js new file mode 100644 index 0000000..a1a28e3 --- /dev/null +++ b/database/models/users.js @@ -0,0 +1,25 @@ +module.exports = (sequelize, DataTypes) => { + return sequelize.define('users', { + user_id: { + type: DataTypes.STRING, + primaryKey: true, + }, + quizLana: { + type: DataTypes.INTEGER, + defaultValue: 0, + allowNull: false, + }, + quizJulia: { + type: DataTypes.INTEGER, + defaultValue: 0, + allowNull: false + }, + quizUltimo: { + type: DataTypes.STRING, + defaultValue: "Sem Histórico", + allowNull: false + } + }, { + timestamps: false, + }); +};