Browse Source

Updated feature-preferences

pull/43/head
Eric Yang 3 years ago
parent
commit
cb8c553707
  1. 63
      src/helpers/db.js
  2. 8
      src/helpers/env.js
  3. 8
      src/helpers/env.ts
  4. 34
      src/helpers/log.js
  5. 82
      src/helpers/util/pagination.js

63
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;

8
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';

8
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';
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';

34
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;

82
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;
Loading…
Cancel
Save