mirror of
https://github.com/ION606/selmerBot.git
synced 2026-05-15 05:36:54 +00:00
Added error reporting functionality, fixed bugs in the various game's code and tidied up a bit
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
//@ts-check
|
||||
const { addxp, STATE, BASE } = require("../db/econ");
|
||||
const turnManger = require('../turnManager.js');
|
||||
const { addComplaintButton } = require('../dev only/submitcomplaint');
|
||||
|
||||
|
||||
//#region game lose/win
|
||||
@@ -8,7 +9,11 @@ function loseGame(user_dbo, xp_collection, message, bot = null) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
user_dbo.find({"game": {$exists: true}}).toArray(function(err, docs){
|
||||
const doc = docs[0];
|
||||
if (doc == undefined) { return message.reply("Oops! There's been an error! Please contact support if this problem persists!"); }
|
||||
if (doc == undefined) {
|
||||
message.reply("Oops! There's been an error, click the ✅ to report this!");
|
||||
addComplaintButton(bot, message);
|
||||
return;
|
||||
}
|
||||
if (doc.game == null) { return message.reply("You're not even in a game and you're trying to quit! Sad..."); }
|
||||
|
||||
var addbal;
|
||||
@@ -49,7 +54,7 @@ function winGame(client, bot, db, user_dbo, xp_collection, message) {
|
||||
}
|
||||
|
||||
//Delete the bot's record of the game
|
||||
client.db('B|S' + bot.user.id).collection(user_dbo.s.namespace.db.substr(0, user_dbo.s.namespace.db.length - 6)).drop();
|
||||
client.db('B|S' + bot.user.id).collection(message.guild.id).drop();
|
||||
|
||||
|
||||
//Update the player with xp
|
||||
|
||||
@@ -6,6 +6,7 @@ let ecoimport = require("../db/econ.js");
|
||||
//#region Game Imports
|
||||
const battle = require("./battle.js");
|
||||
const ttt = require('./tictactoe.js');
|
||||
const trivia = require('./trivia.js');
|
||||
|
||||
//#endregion
|
||||
|
||||
@@ -147,7 +148,7 @@ function equip(message, args, command, dbo, bot, shop) {
|
||||
|
||||
if (doc.game != null) {
|
||||
ret = true;
|
||||
console.log(doc.game);
|
||||
// console.log(doc.game);
|
||||
return message.reply('You can\'t equip while in a game!');
|
||||
}
|
||||
|
||||
@@ -357,6 +358,8 @@ module.exports ={
|
||||
|
||||
//#region game-specific commands
|
||||
else {
|
||||
if (command == undefined) { return message.reply("Please specify a game or use _!game help_"); }
|
||||
|
||||
//Make change to new name if necessary
|
||||
if (command.replaceAll(" ", "").toLowerCase() == 'tictactoe') { command = 'Tic Tac Toe'; }
|
||||
|
||||
@@ -377,12 +380,14 @@ module.exports ={
|
||||
}
|
||||
|
||||
message.channel.send(`${other_discord}, <@${message.author.id}> has invited you to play _"Tic Tac Toe"_. To accept, please reply to this message with _!game accept_`);
|
||||
} else if (game == 'trivia' || command == 'trivia') {
|
||||
trivia.execute(message, args, Discord, client, bot);
|
||||
}
|
||||
|
||||
|
||||
//Catch statement (invalid command)
|
||||
else {
|
||||
if (command == undefined) { message.reply("Please specify a game or use _!game help_"); }
|
||||
else { message.reply(`'!game ${command}' is not a command!`); }
|
||||
message.reply(`'${bot.prefix}game ${command}' is not a command!`);
|
||||
}
|
||||
}
|
||||
//#endregion
|
||||
|
||||
@@ -108,7 +108,7 @@ function postActionBar(interaction, user_dbo, board, won, initial = false) {
|
||||
}
|
||||
}
|
||||
|
||||
console.log(componentlist);
|
||||
// console.log(componentlist);
|
||||
|
||||
if (initial) {
|
||||
interaction.send({ content: `Your turn <@${user_dbo.s.namespace.collection}>!`, components: componentlist });
|
||||
@@ -124,12 +124,15 @@ async function handle(client, db, dbo, other, bot, thread, command, doc, interac
|
||||
let board = ["", "", "", "", "", "", "", "", ""];
|
||||
postActionBar(thread, dbo, board, false,true);
|
||||
} else {
|
||||
|
||||
//Change the board
|
||||
let square = Number(interaction.customId.split('|')[1]);
|
||||
let symbol = doc.symbols[doc.turn];
|
||||
let board = doc.board;
|
||||
board[square] = symbol;
|
||||
client.db('B|S' + bot.user.id).collection(dbo.s.namespace.db.substr(0, dbo.s.namespace.db.length - 6)).updateOne({'board': {$exists: true}}, {$set: {board: board}});
|
||||
const gamedbo = client.db('B|S' + bot.user.id).collection(interaction.guildId);
|
||||
|
||||
gamedbo.updateOne({$or: [ {0: interaction.user.id}, {1: interaction.user.id} ], 'board': {$exists: true}}, {$set: {board: board}});
|
||||
|
||||
//Check if the game is over
|
||||
let won = isTerminal(board);
|
||||
|
||||
+25
-13
@@ -47,7 +47,15 @@ function changeDB(bot, message, m) {
|
||||
* @param {int} time
|
||||
*/
|
||||
function startTrivia(message, m, time, bot) {
|
||||
const obj = m.values().next().value;
|
||||
var iter = m.values().next();
|
||||
var obj = iter.value;
|
||||
|
||||
//Get rid of the "answers required" ones
|
||||
while (obj.question.toLowerCase().indexOf('which of these') != -1 && obj.question.toLowerCase().indexOf('which of the following') != -1) {
|
||||
iter = iter.next();
|
||||
obj = iter.value;
|
||||
}
|
||||
|
||||
const question = obj.question;
|
||||
const answer = obj.answer;
|
||||
console.log(answer);
|
||||
@@ -57,8 +65,11 @@ function startTrivia(message, m, time, bot) {
|
||||
return (response.content.toLowerCase() == answer.toLowerCase());
|
||||
};
|
||||
|
||||
message.reply({ content: question, fetchReply: true })
|
||||
message.reply({ content: `${question}\n(Type your answers below!)`, fetchReply: true })
|
||||
.then(() => {
|
||||
const timeList = ['🔟', '9️⃣', '8️⃣', '7️⃣', '6️⃣', '5️⃣', '4️⃣', '3️⃣', '2️⃣', '1️⃣', '0️⃣' ];
|
||||
var i = 0;
|
||||
const intId = setInterval(() => { if (i < timeList.length) { message.react(timeList[i]); i++ } }, Math.round(time/11));
|
||||
//time: 1000 = 1 second
|
||||
message.channel.awaitMessages({ filter, max: 10, time: time }) // , errors: ['time']
|
||||
.then((collected) => {
|
||||
@@ -68,22 +79,23 @@ function startTrivia(message, m, time, bot) {
|
||||
message.reply('Tsk Tsk, looks like nobody got the answer this time.');
|
||||
}
|
||||
|
||||
changeDB(bot, message, null);
|
||||
// changeDB(bot, message, null);
|
||||
clearInterval(intId);
|
||||
})
|
||||
.catch((collected) => {
|
||||
console.log(collected);
|
||||
message.reply('Tsk Tsk, looks like nobody got the answer this time.');
|
||||
changeDB(bot, message, null);
|
||||
// changeDB(bot, message, null);
|
||||
clearInterval(intId);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
//Add shuffle button
|
||||
//Add shuffle button?
|
||||
|
||||
module.exports = {
|
||||
name: 'trivia',
|
||||
description: 'Play a game of Trivia with yourself or others! - (use _trivia help_)',
|
||||
async execute(message, args, Discord, Client, bot) {
|
||||
const difficult = ['easy', 'medium', 'hard'];
|
||||
let inputs = ['easy', ''];
|
||||
@@ -93,7 +105,7 @@ module.exports = {
|
||||
} else if (args[0] == 'help') {
|
||||
let temp = `Use ${bot.prefix}trivia [difficulty (easy, medium, hard)] [topic] [time]\n`;
|
||||
temp += '**__Trivia Categories__**\n';
|
||||
m.forEach((val, key) => {
|
||||
categories.forEach((val, key) => {
|
||||
temp += `_${key}_\n`;
|
||||
})
|
||||
temp += '_Please copy and paste the FULL NAME if you want to use a category';
|
||||
@@ -110,7 +122,7 @@ module.exports = {
|
||||
// const json = await a.json();
|
||||
// console.log(json);
|
||||
|
||||
var url = `https://opentdb.com/api.php?amount=${5}&difficulty=${inputs[0]}&type=multiple`;
|
||||
var url = `https://opentdb.com/api.php?amount=${3}&difficulty=${inputs[0]}&type=multiple`;
|
||||
if (inputs[1] != '') {
|
||||
url += `&category=${inputs[1]}`;
|
||||
}
|
||||
@@ -146,12 +158,12 @@ module.exports = {
|
||||
i ++;
|
||||
});
|
||||
|
||||
const time = args[2] || (difficult[0].indexOf(inputs[0]) + 1) * 10000;
|
||||
changeDB(bot, message, m);
|
||||
const time = args[2] || (difficult.indexOf(inputs[0]) + 1) * 10000;
|
||||
|
||||
// console.log(m, time);
|
||||
// changeDB(bot, message, m);
|
||||
startTrivia(message, m, time, bot);
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user