From cb8c553707cd4e69c68a8468c5bc354925f1bb27 Mon Sep 17 00:00:00 2001 From: Eric Yang <64386991+xlq902@users.noreply.github.com> Date: Thu, 7 Apr 2022 18:16:02 -0400 Subject: [PATCH] Updated feature-preferences --- src/helpers/db.js | 63 ++++++++++++++++++++++++++ src/helpers/env.js | 8 ++++ src/helpers/env.ts | 8 ++-- src/helpers/log.js | 34 ++++++++++++++ src/helpers/util/pagination.js | 82 ++++++++++++++++++++++++++++++++++ 5 files changed, 191 insertions(+), 4 deletions(-) create mode 100644 src/helpers/db.js create mode 100644 src/helpers/env.js create mode 100644 src/helpers/log.js create mode 100644 src/helpers/util/pagination.js diff --git a/src/helpers/db.js b/src/helpers/db.js new file mode 100644 index 0000000..12116ac --- /dev/null +++ b/src/helpers/db.js @@ -0,0 +1,63 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.connect = exports.updateScore = void 0; +const mongoose_1 = __importDefault(require("mongoose")); +const log_1 = __importDefault(require("../helpers/log")); +const userScore_1 = __importDefault(require("../models/userScore")); +function updateScore(isCorrect, score, authorId) { + return __awaiter(this, void 0, void 0, function* () { + if (!isCorrect) { + return `Nice try! Your score is still ${score}.`; + } + else { + // TODO: Error handling + const doc = yield userScore_1.default.findOne({ + authorID: authorId, + }); + if (!doc) { + const newUserScore = new userScore_1.default({ + authorID: authorId, + score: score + 4, + }); + newUserScore.save(err => { + if (err) { + (0, log_1.default)({ logger: 'db', content: `Error creating new user ${authorId} for scoring`, level: 'error' }); + } + else { + (0, log_1.default)({ logger: 'db', content: `Successfully created user ${authorId} for scoring`, level: 'debug' }); + } + }); + } + else { + doc.score = doc.score + 4; + doc.save(); + } + return `Great job! Your score is now ${score + 4}.`; + } + }); +} +exports.updateScore = updateScore; +function connect(mongoUri) { + return __awaiter(this, void 0, void 0, function* () { + mongoose_1.default + .connect(mongoUri, { + useUnifiedTopology: true, + useNewUrlParser: true, + }) + .then(() => (0, log_1.default)({ logger: 'db', content: `Connected to the database at ${mongoUri}!`, level: 'info' })) + .catch(err => (0, log_1.default)({ logger: 'db', content: `Failed to connect to the database at ${mongoUri}: ${err}`, level: 'fatal' })); + }); +} +exports.connect = connect; diff --git a/src/helpers/env.js b/src/helpers/env.js new file mode 100644 index 0000000..f4d6a12 --- /dev/null +++ b/src/helpers/env.js @@ -0,0 +1,8 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.mongoUri = exports.token = exports.testingGuild = exports.clientId = void 0; +require("dotenv/config"); +exports.clientId = process.env.CLIENT_ID || ''; +exports.testingGuild = process.env.TESTING_GUILD || ''; +exports.token = process.env.TOKEN || ''; +exports.mongoUri = process.env.MONGO_URI || 'mongodb://localhost:27017'; diff --git a/src/helpers/env.ts b/src/helpers/env.ts index c339592..25d4f8e 100644 --- a/src/helpers/env.ts +++ b/src/helpers/env.ts @@ -1,6 +1,6 @@ import 'dotenv/config'; -export const clientId : string = process.env.CLIENT_ID || ''; -export const testingGuild : string = process.env.TESTING_GUILD || ''; -export const token : string = process.env.TOKEN || ''; -export const mongoUri : string = process.env.MONGO_URI || 'mongodb://mongo:27017/awesome'; \ No newline at end of file +export const clientId = process.env.CLIENT_ID || ''; +export const testingGuild = process.env.TESTING_GUILD || ''; +export const token = process.env.TOKEN || ''; +export const mongoUri = process.env.MONGO_URI = 'mongodb://localhost:27017'; diff --git a/src/helpers/log.js b/src/helpers/log.js new file mode 100644 index 0000000..abb6021 --- /dev/null +++ b/src/helpers/log.js @@ -0,0 +1,34 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const log4js_1 = __importDefault(require("log4js")); +function default_1(config) { + const logger = log4js_1.default.getLogger(config.logger); + logger.level = 'debug'; + switch (config.level) { + case 'trace': + logger.trace(config.content); + break; + case 'debug': + logger.debug(config.content); + break; + case 'info': + logger.info(config.content); + break; + case 'warn': + logger.warn(config.content); + break; + case 'error': + logger.error(config.content); + break; + case 'fatal': + logger.fatal(config.content); + break; + default: + logger.debug(config.content); + break; + } +} +exports.default = default_1; diff --git a/src/helpers/util/pagination.js b/src/helpers/util/pagination.js new file mode 100644 index 0000000..a4576cd --- /dev/null +++ b/src/helpers/util/pagination.js @@ -0,0 +1,82 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.paginateInteraction = exports.paginateMessage = void 0; +const discord_js_1 = require("discord.js"); +function paginateMessage(message, embeds) { + return __awaiter(this, void 0, void 0, function* () { + let index = 0; + const row = new discord_js_1.MessageActionRow; + row.addComponents(new discord_js_1.MessageButton() + .setCustomId('paginator-left') + .setEmoji('868552005977788466') + .setStyle('SECONDARY'), new discord_js_1.MessageButton() + .setCustomId('paginator-right') + .setEmoji('868551772887711754') + .setStyle('SECONDARY')); + yield message.reply({ content: `Page 1 of ${embeds.length}:`, embeds: [embeds[index]], components: [row] }) + .then((paginatorMessage) => __awaiter(this, void 0, void 0, function* () { + const filter = m => m.author.id === message.author.id; + const paginatorCollector = paginatorMessage.createMessageComponentCollector({ componentType: 'BUTTON', filter: filter }); + paginatorCollector.on('collect', (i) => __awaiter(this, void 0, void 0, function* () { + switch (i.customId) { + case 'paginator-left': + index--; + if (index < 0) + index = embeds.length - 1; + break; + case 'paginator-right': + index++; + if (index > embeds.length - 1) + index = 0; + break; + } + paginatorMessage.edit({ content: `Page ${index + 1} of ${embeds.length}:`, embeds: [embeds[index]] }); + })); + })); + }); +} +exports.paginateMessage = paginateMessage; +function paginateInteraction(interaction, embeds) { + return __awaiter(this, void 0, void 0, function* () { + let index = 0; + const row = new discord_js_1.MessageActionRow; + row.addComponents(new discord_js_1.MessageButton() + .setCustomId('paginator-left') + .setEmoji('868552005977788466') + .setStyle('SECONDARY'), new discord_js_1.MessageButton() + .setCustomId('paginator-right') + .setEmoji('868551772887711754') + .setStyle('SECONDARY')); + yield interaction.followUp({ content: `Page 1 of ${embeds.length}:`, embeds: [embeds[index]], components: [row], fetchReply: true }) + .then((p) => __awaiter(this, void 0, void 0, function* () { + const paginatorMessage = p; + const filter = i => i.user.id === interaction.user.id; + const paginatorCollector = paginatorMessage.createMessageComponentCollector({ componentType: 'BUTTON', filter: filter }); + paginatorCollector.on('collect', (i) => __awaiter(this, void 0, void 0, function* () { + switch (i.customId) { + case 'paginator-left': + index--; + if (index < 0) + index = embeds.length - 1; + break; + case 'paginator-right': + index++; + if (index > embeds.length - 1) + index = 0; + break; + } + yield i.update({ content: `Page ${index + 1} of ${embeds.length}:`, embeds: [embeds[index]] }); + })); + })); + }); +} +exports.paginateInteraction = paginateInteraction;