Skip to content
🤔prompts chat🧠
🔍
question:// language: JavaScript const http = require('http'); const express = require('express'); const app = express(); const Discord = require('discord.js'); const Datastore = require('nedb'); var db = new Datastore({ filename: '.data/datafile', autoload: true }); const client = new Discord.Client(); var currentGuildVars=null; var serversObj=[]; var serversList=[]; var waitingPlayersList=[]; var playerList=[]; var mafiaChannel=null; var gameState=-1; var gameTime=0; var logicGameTime=0; var defensePlayer=null; var guiltCount=0; var timeController=null; var findChannelString=null; var numPlayers = 5; var testTimer = null; var activeGames = []; const MAFIA = 0; const DETECTIVE = 1; const TOWN = 2; const DOCTOR = 3; const NOGAME = -1; const DAY = 0; const NIGHT = 1; const DEFENSE = 2; const VOTING = 3; const GAMESTARTING = 4; const ENDGAME = 5; const DAYLENGTH = 180; const NIGHTLENGTH = 60; const DEFENSELENGTH = 30; const VOTINGLENGTH = 30; const prefix ='m.'; function getRandomInt(max) { return Math.floor(Math.random() * Math.floor(max)); } //makes vars into objs function ServerObj(id) { return { id: id, waitingPlayersList: waitingPlayersList, playerList: playerList, mafiaChannel: mafiaChannel, gameState: gameState, gameTime: gameTime, logicGameTime: logicGameTime, defensePlayer: defensePlayer, guiltCount: guiltCount, timeController: timeController, findChannelString: findChannelString, testTimer: testTimer }; } //update the data function updateData(id,varName,amt) { var found = false; db.find({ _id: id }, function (err, docs) { if(docs.length==0) found=true; }); if(found){ var foundData; db.find({ _id: id }, function (err, docs) { if(docs.length==0) foundData=docs; }); foundData[0][varName]=amt; } } function convertSecToMinSec(sec) { if(sec < 60) { return sec + sec; } var minutes = Math.floor(sec / 60); sec -= minutes * 60; return minutes + min and + sec + sec; } //given a array and a item. Put the item in a random position in the array. function placeItemRandomlyInArray(array, item) { if(array.length == 0) { array.push(item); } else { var ranNum = getRandomInt(array.length + 1); array.splice(ranNum,0,item); } } //given a string of a id returns the player obj with that name. Return null if player isn't found. function findPlayerWithId(id) { for(var i = 0; i < currentGuildVars.playerList.length; i++) { if(currentGuildVars.playerList[i].name.id == id) { return currentGuildVars.playerList[i]; } } return null; } function sendInstructions(name) { var tempText = Game Commands: n + m.play - join a game n + m.quit - quit the game n + m.check - checks the players playing the game (only useable ingame) n + m.getrules - send instructions to user via pm n + m.time - gets the current time (only useable ingame) n; name.send(tempText); } function assignRoles() { // //for the rest of the players make them town people. // while(waitingPlayersList.length > 0) // { // var randNum = getRandomInt(waitingPlayersList.length); var numOfDoc = 1; var numOfDete = 1; var numOfMafia = Math.floor((currentGuildVars.waitingPlayersList.length - 2) / 3); if(numOfMafia < 2) numOfDoc = 0; //Other options for less then 5 players if(currentGuildVars.waitingPlayersList.length < 5) { numOfMafia = 1; numOfDoc = 0; numOfDete = 1; } var indexes = []; for(var i = 0; i < currentGuildVars.waitingPlayersList.length; i++) { indexes.push(i); } while(numOfDoc >= 1) { var randNum = getRandomInt(indexes.length); placeItemRandomlyInArray(currentGuildVars.playerList, new Player(currentGuildVars.waitingPlayersList[indexes[randNum]], DOCTOR)); indexes.splice(randNum, 1); numOfDoc--; } while(numOfDete >= 1) { //something like? var randNum = getRandomInt(indexes.length); placeItemRandomlyInArray(currentGuildVars.playerList, new Player(currentGuildVars.waitingPlayersList[indexes[randNum]], DETECTIVE)); indexes.splice(randNum, 1); numOfDete--; } while(numOfMafia >= 1) { var randNum = getRandomInt(indexes.length); placeItemRandomlyInArray(currentGuildVars.playerList, new Player(currentGuildVars.waitingPlayersList[indexes[randNum]], MAFIA)); indexes.splice(randNum, 1); numOfMafia--; } while(indexes.length > 0) { var randNum = getRandomInt(indexes.length); placeItemRandomlyInArray(currentGuildVars.playerList, new Player(currentGuildVars.waitingPlayersList[indexes[randNum]], TOWN)); indexes.splice(randNum, 1); } } function checkWin() { var aliveMafia = 0; var aliveInnocent = 0; for(var i = 0; i < currentGuildVars.playerList.length; i++) { if(currentGuildVars.playerList[i].role == MAFIA && currentGuildVars.playerList[i].alive) { aliveMafia++; } if(currentGuildVars.playerList[i].role!= MAFIA && currentGuildVars.playerList[i].alive) { aliveInnocent++; } } if(aliveMafia == 0) { currentGuildVars.mafiaChannel.send(Town Wins!); currentGuildVars.mafiaChannel.send(Number Of Mafia Left: + aliveMafia + Number Of Town Left: + aliveInnocent); return true; } if(aliveMafia >= aliveInnocent) { //mafia wins. currentGuildVars.mafiaChannel.send(Maifa Wins!); currentGuildVars.mafiaChannel.send(Number Of Mafia Left: + aliveMafia + Number Of Town Left: + aliveInnocent); return true; } return false; } //returns a array of all the mafia players that are still alive. function getAllMafiaPlayers() { var mafiaP = []; for(var i = 0; i < currentGuildVars.playerList.length; i++) { var tempCurrentPlayer = currentGuildVars.playerList[i]; if(tempCurrentPlayer.role == MAFIA && tempCurrentPlayer.alive) { mafiaP.push(tempCurrentPlayer); } } return mafiaP; } //returns an array of all alive players function getAllAlivePlayers() { var mafiaP = []; for(var i = 0; i < currentGuildVars.playerList.length; i++) { if(currentGuildVars.playerList[i].alive) { mafiaP.push(currentGuildVars.playerList[i]); } } return mafiaP; } //returns a array of all the mafia players names that are still alive. function getAllMafiaPlayersNames() { var mafiaP = []; for(var i = 0; i < currentGuildVars.playerList.length; i++) { if(currentGuildVars.playerList[i].role == MAFIA && currentGuildVars.playerList[i].alive) { mafiaP.push(currentGuildVars.playerList[i].name); } } return mafiaP; } function getMafiaCount() { var count = 0; for(var i = 0; i < currentGuildVars.playerList.length; i++) { if(currentGuildVars.playerList[i].role == MAFIA && currentGuildVars.playerList[i].alive) { count++; } } return count; } function getPlayerOf(name) { for(var i = 0; i < serversObj.length; i++) { var currentGuild = serversObj[i]; for(var j = 0; j < currentGuild.playerList.length; j++) { if(currentGuild.playerList[j].name == name) return currentGuild.playerList[j]; } } return null; } //Returns the player that was voted to be killed by the mafia. function getKilledPlayer() { //This is the list of players that has been voted to kill. var listOfPlayersVoted = []; for(var i = 0; i < currentGuildVars.playerList.length; i++) { var currPlayer = currentGuildVars.playerList[i]; if(currPlayer.alive && currPlayer.role == MAFIA) { if(currPlayer.kill!= null) { currPlayer.kill.voteKill++; listOfPlayersVoted.push(currPlayer.kill); currPlayer.kill = null; } } } //todo:with the list of votekill players return one that is killed. if(listOfPlayersVoted.length == 0) return null; return listOfPlayersVoted[getRandomInt(listOfPlayersVoted.length)]; } function getHealedPlayer() { for(var i = 0; i < currentGuildVars.playerList.length; i++) { if(currentGuildVars.playerList[i].alive && currentGuildVars.playerList[i].role == DOCTOR) { if(currentGuildVars.playerList[i].healed == null) { currentGuildVars.playerList[i].healedLastRound = null; return null; } var healedPerson = currentGuildVars.playerList[i].healed; currentGuildVars.playerList[i].healedLastRound = healedPerson; currentGuildVars.playerList[i].healed = null; return healedPerson; } } return null; } function getDoctor() { for(var i = 0; i < currentGuildVars.playerList.length; i++) { if(currentGuildVars.playerList[i].alive && currentGuildVars.playerList[i].role == DOCTOR) { return currentGuildVars.playerList[i]; } } return null; } //gets the person that was investigated. //return null if no one was investigated. function tellTheDetectiveWhoHeSearched() { for(var i = 0; i < currentGuildVars.playerList.length; i++) { if(currentGuildVars.playerList[i].alive && currentGuildVars.playerList[i].role == DETECTIVE) { var investigatedPlayer = currentGuildVars.playerList[i].searched; currentGuildVars.playerList[i].searched = null; if(investigatedPlayer!= null) { if(investigatedPlayer.role == MAFIA) currentGuildVars.playerList[i].name.send(The person you investigated was a mafia.); else currentGuildVars.playerList[i].name.send(The person you investigated was not a mafia.); } } } } function isPlayerInGame(name) { for(var i = 0; i < serversObj.length; i++) { var currentGuild = serversObj[i]; for(var j = 0; j < currentGuild.waitingPlayersList.length; j++) { if(currentGuild.waitingPlayersList[j] == name) return true; } for(var j = 0; j < currentGuild.playerList.length; j++) { if(currentGuild.playerList[j].name == name) return true; } } return false; } function getServerOfPlayer(player) { for(var i = 0; i < serversObj.length; i++) { var currentGuild = serversObj[i]; if(currentGuild.playerList.indexOf(player)!= -1) return serversObj[i]; } return null; } //resets all variables to their default. function resetVariables() { currentGuildVars.waitingPlayersList=[]; currentGuildVars.playerList=[]; if(currentGuildVars.mafiaChannel!= null) currentGuildVars.mafiaChannel.delete(); currentGuildVars.mafiaChannel = null; currentGuildVars.gameState=-1; currentGuildVars.gameTime=0; currentGuildVars.logicGameTime=0; currentGuildVars.defensePlayer=null; currentGuildVars.guiltCount=0; currentGuildVars.findChannelString=mafia; if(currentGuildVars.timeController!= null) { clearTimeout(currentGuildVars.timeController); currentGuildVars.timeController = null; } } function sendNightInstructions() { for(var i = 0; i < currentGuildVars.playerList.length; i++) { if(currentGuildVars.playerList[i].alive) { if(currentGuildVars.playerList[i].role == MAFIA) { currentGuildVars.playerList[i].name.send(You are allowed to kill one player. n Use m.check to check player numbers. n Use m.kill {their number} to vote kill someone); } if(currentGuildVars.playerList[i].role == DETECTIVE) { currentGuildVars.playerList[i].name.send(You can investigate one player at night. n Use m.check to check player numbers. n Use m.investigate {their number} to investigate someone. n In the morning you will get your result); } if(currentGuildVars.playerList[i].role == DOCTOR) { currentGuildVars.playerList[i].name.send(You can heal one player at night. n Use m.check to check player numbers. n Use m.heal {their number} to heal someone.); } if(currentGuildVars.playerList[i].role == TOWN) { currentGuildVars.playerList[i].name.send(It is night. Stay safe until morning); } } } } function sendHowToPlayInstructions(name) { var instructionText = ; instructionText += Hello + name.username + ! Welcome to the game of Mafia. n; instructionText += Overview: n In Mafia there are two groups of people, the mafia(s) and the innocents. At the beginning of the game you are assigned a role (your role will be sent to you by private message from me + client.user.username + . If you're a mafia your goal is to kill non-mafias until there is only the same amount of mafias as non-mafias (Ex. if there are 2 mafias, your goal is to kill until there are only 2 non-mafias left to win). + If you a non-mafia your goal is to kill all mafias. n; instructionText += ; name.send(instructionText); } function Player(name,role) { this.name = name; this.voteCount = 0; this.voteKill = 0; this.role = role; this.hasVoted = false; this.alive = true; this.votedWhat = null; this.searched = null; this.healed = null; this.healedLastRound = null; this.kill = null; this.detectivePm = null; } /*Handlers*/ client.on('ready',() => { console.log('I'm OnlinenI'm Online'); client.user.setPresence({ status: 'online', game: { name:'m.play || m.help' } }); }); client.on('message', message => { var server = message.guild; var name = message.author; var currentPlayer = getPlayerOf(name); if(serversList.indexOf(server)>-1){ currentGuildVars=serversObj[serversList.indexOf(server)]; }else{ serversList.push(server); serversObj.push(new ServerObj(server)); currentGuildVars=serversObj[serversList.indexOf(server)]; resetVariables(); } if(currentPlayer!= null && message.channel.type == dm) currentGuildVars = getServerOfPlayer(currentPlayer); //ignore bot's own messages if (name === client.user) return; if(currentGuildVars.gameState==NIGHT && name!= client.user && message.guild!= null){ message.delete(); message.channel.send('Shhhhh. It is night time. (Please don't type at night as it ruins the integrity of the game.'); } if(currentGuildVars.gameState==DEFENSE && name!= client.user && defensePlayer.name!=name && message.guild!= null){ message.delete(); message.author.send('Shhhhh. It is defense time. (Please don't type at their defense as it ruins the integrity of the game.'); } if (message.content.startsWith(prefix + 'ping')) { message.channel.send('pong'); } if (message.content.startsWith(prefix + 'help')) { message.channel.send('A help message has been sent to your inbox.'); sendInstructions(message.author); } if (message.content.startsWith(prefix + 'quit')) { if(message.channel.type == text) { if(currentGuildVars.gameState!=NOGAME) { message.channel.send(too bad + name + you can't quit the game.); } else { if(currentGuildVars.waitingPlayersList.indexOf(name) > -1) { currentGuildVars.waitingPlayersList.splice(currentGuildVars.waitingPlayersList.indexOf(name), 1); message.channel.send(name+ has left the queue. Currently +currentGuildVars.waitingPlayersList.length+ players in queue and need a minimum of 5 to start.); } else { message.channel.send(You are not on the player list); } } } else { message.channel.send(You cannot use this command outside of a guild's chat channel); } } if(message.content.startsWith(prefix + 'getrules')) { sendHowToPlayInstructions(name); if(message.channel.type == text) message.channel.send(Instruction sent to + name + . Please check your private message.); } if(message.content.startsWith(prefix + 'time') && currentGuildVars.gameState!= NOGAME && currentPlayer!= null && currentPlayer.alive) { var timeUntilItEnds = ; var currentSetting = ; if(currentGuildVars.gameState == DAY) { timeUntilItEnds = convertSecToMinSec(DAYLENGTH - currentGuildVars.logicGameTime); currentSetting = day; } if(currentGuildVars.gameState == NIGHT) { timeUntilItEnds = convertSecToMinSec(NIGHTLENGTH - currentGuildVars.logicGameTime); currentSetting = night; } if(currentGuildVars.gameState == DEFENSE) { timeUntilItEnds = convertSecToMinSec(DEFENSELENGTH - currentGuildVars.logicGameTime); currentSetting = player defense; } if(currentGuildVars.gameState == VOTING) { timeUntilItEnds = convertSecToMinSec(VOTINGLENGTH - currentGuildVars.logicGameTime); currentSetting = voting; } message.channel.send(timeUntilItEnds + remaining until + currentSetting + ends.); } if(message.content.startsWith(prefix + 'tmagic') && currentGuildVars.gameState!= NOGAME && currentPlayer!= null && currentPlayer.alive && message.author.id==process.env.BOTTOKEN) { var inputNum = parseInt(message.content.split( )[1]); if(!isNaN(inputNum)) { if(inputNum < -60) inputNum = -60; if(inputNum > 60) inputNum = 60; currentGuildVars.logicGameTime += inputNum; if(currentGuildVars.logicGameTime < 0) currentGuildVars.logicGameTime = 0; if(inputNum > 0) currentGuildVars.mafiaChannel.send(currentPlayer.name + used time magic to remove + Math.abs(inputNum) + sec from the clock.); else if(inputNum < 0) currentGuildVars.mafiaChannel.send(currentPlayer.name + used time magic to add + Math.abs(inputNum) + sec to the clock. ); } } if (message.content.startsWith(prefix +'reset') && message.author.id==process.env.BOTTOKEN) { message.channel.send(Game Reset); resetVariables(); } if ((currentGuildVars.gameState==GAMESTARTING||currentGuildVars.gameState==NOGAME)&&message.content.startsWith(prefix + 'play')) { if(message.channel.type == text) { if(!isPlayerInGame(name)) { { message.channel.send(name+ has joined the queue. Currently +currentGuildVars.waitingPlayersList.length+ players in queue and need a minimum of +numPlayers+ to start.); } } else { message.channel.send(You are already in a game, cannot join another game.); } } else { message.channel.send(You cannot use this command outside of a guild's chat channel); } //met player requirments and game isnt already starting if(currentGuildVars.waitingPlayersList.length>=numPlayers && currentGuildVars.gameState==NOGAME) { activeGames.push(currentGuildVars); currentGuildVars.gameState = GAMESTARTING; message.channel.send(Enough players have joined the match. You have 10 seconds to join before the ability to join is closed off.); currentGuildVars.findChannelString=mafia; var serverNum=1; while(server.channels.find(name,currentGuildVars.findChannelString)!=null) { currentGuildVars.findChannelString = mafia- + serverNum; serverNum++; } server.createChannel(currentGuildVars.findChannelString, 'text', [ id: server.id, deny: ['MANAGE_MESSAGES','CREATE_INSTANT_INVITE','MANAGE_CHANNELS','SEND_MESSAGES','MANAGE_MESSAGES','MANAGE_ROLES','MANAGE_WEBHOOKS','MENTION_EVERYONE'], allow: ['ADD_REACTIONS','VIEW_CHANNEL','READ_MESSAGE_HISTORY','EMBED_LINKS','ATTACH_FILES','SEND_TTS_MESSAGES','USE_EXTERNAL_EMOJIS'] }]); var timeController=setInterval(function() { var tempcurrentGuildVars=currentGuildVars; for(var k in activeGames){ currentGuildVars=activeGames[k]; currentGuildVars.gameTime++; currentGuildVars.logicGameTime++; if(currentGuildVars.gameTime>=10&&currentGuildVars.gameState==GAMESTARTING){ currentGuildVars.gameState=DAY; currentGuildVars.mafiaChannel = currentGuildVars.id.channels.find(name,currentGuildVars.findChannelString); currentGuildVars.mafiaChannel.send(Joining closed, Game starting : Players in game + currentGuildVars.waitingPlayersList.join() ); currentGuildVars.mafiaChannel.send(The mafia is trying to kill members of the town! As town villagers, vote to kill the mafia and keep the town safe! You have 3 mins to chat and vote before night falls.n Use m.check to see which number corresponds to which player and use m.vote <playerNumber> to vote for them(Use m.cancelvote to cancel your vote). You will be sleeping at night and won't be able to talk.); message.channel.send(The game has started. No more players are allowed to join); assignRoles(); var mafiaAmt = getMafiaCount(); //playerList.push(new Player(waitingPlayersList[0],3)); for(var i in currentGuildVars.playerList){ currentGuildVars.mafiaChannel.overwritePermissions(currentGuildVars.playerList[i].name, { SEND_MESSAGES: true }); currentGuildVars.playerList[i].name.send(You are playing on: + currentGuildVars.id.name); if(currentGuildVars.playerList[i].role==MAFIA&&mafiaAmt==1){ currentGuildVars.playerList[i].name.send(You are the mafia and can kill 1 person a night.); } if(currentGuildVars.playerList[i].role==MAFIA&&mafiaAmt>1){ currentGuildVars.playerList[i].name.send(You are the mafia and can kill 1 person a night. Your teammate(s) is(are) +getAllMafiaPlayersNames().join()); } if(currentGuildVars.playerList[i].role==DOCTOR){ currentGuildVars.playerList[i].name.send(You are the doctor and can heal 1 person a night but you can't heal the same person twice in a row.); } if(currentGuildVars.playerList[i].role==DETECTIVE){ currentGuildVars.playerList[i].name.send(You are the detective and can investigate 1 person a night.); } if(currentGuildVars.playerList[i].role==TOWN){ currentGuildVars.playerList[i].name.send(You are part of the town and need to find the mafia.); } } //resets gameTime after setup currentGuildVars.gameTime=0; currentGuildVars.logicGameTime=0; } if(currentGuildVars.logicGameTime>=DAYLENGTH&&currentGuildVars.gameState==DAY) { currentGuildVars.mafiaChannel.send(It is now night.); sendNightInstructions(); currentGuildVars.gameState=NIGHT; for(var i in currentGuildVars.playerList){ currentGuildVars.mafiaChannel.overwritePermissions(currentGuildVars.playerList[i].name, { SEND_MESSAGES: false }); } currentGuildVars.logicGameTime=0; } if(currentGuildVars.logicGameTime>=NIGHTLENGTH&&currentGuildVars.gameState==NIGHT) { currentGuildVars.mafiaChannel.send(It is now day.); currentGuildVars.gameState=DAY; tellTheDetectiveWhoHeSearched(); var playerHealed = getHealedPlayer(); var playerKilled = getKilledPlayer(); var tempDocVar = getDoctor(); if(tempDocVar!= null) { if(playerHealed!= null) { if(playerKilled == playerHealed) { tempDocVar.send(You managed to save + playerHealed.name + from the mafia.); } else { tempDocVar.send(You didn't heal anyone); } } else { tempDocVar.send(You didn't heal anyone); } } if(playerKilled == null || (playerKilled == playerHealed)) { currentGuildVars.mafiaChannel.send(No one was killed.); } else { currentGuildVars.mafiaChannel.send(playerKilled.name + was killed.); playerKilled.alive = false; currentGuildVars.mafiaChannel.overwritePermissions(playerKilled.name, { SEND_MESSAGES: false }); } for(var i in currentGuildVars.playerList){ if(true){ currentGuildVars.playerList[i].voteCount = 0; currentGuildVars.playerList[i].hasVoted = false; currentGuildVars.playerList[i].votedWhat = null; currentGuildVars.defensePlayer=null; currentGuildVars.guiltCount=0; currentGuildVars.mafiaChannel.overwritePermissions(currentGuildVars.playerList[i].name, { SEND_MESSAGES: true }); } } if(checkWin()){ currentGuildVars.gameState=ENDGAME; currentGuildVars.mafiaChannel.send(All players will have chat functions now. 2 mins until the game deletes itself. Thanks for playing and remember to upvote in discords bots); } currentGuildVars.logicGameTime=0; } if(currentGuildVars.gameState==DEFENSE) { if(currentGuildVars.logicGameTime==1) currentGuildVars.mafiaChannel.send(It is now +currentGuildVars.defensePlayer.name+'s defense.); for(var i in currentGuildVars.playerList){ if(currentGuildVars.playerList[i]!=currentGuildVars.defensePlayer){ currentGuildVars.mafiaChannel.overwritePermissions(currentGuildVars.playerList[i].name, { SEND_MESSAGES: false }); } } if(currentGuildVars.logicGameTime>=DEFENSELENGTH){ currentGuildVars.mafiaChannel.send(Your time is up, the town will have to vote on your fate.); currentGuildVars.gameState=VOTING; currentGuildVars.logicGameTime=0; } } if(currentGuildVars.gameState==VOTING) { if(currentGuildVars.logicGameTime==1) currentGuildVars.mafiaChannel.send(Use m.innocent or m.guilty to vote. Use m.cancelvote to cancel your vote); for(var i in currentGuildVars.playerList){ if(currentGuildVars.playerList[i].alive){ currentGuildVars.mafiaChannel.overwritePermissions(currentGuildVars.playerList[i].name, { SEND_MESSAGES: true }); } } if(currentGuildVars.logicGameTime>=VOTINGLENGTH){ if(currentGuildVars.guiltCount<0){ currentGuildVars.defensePlayer.alive=false; currentGuildVars.mafiaChannel.overwritePermissions(currentGuildVars.defensePlayer.name, { SEND_MESSAGES: false }); currentGuildVars.mafiaChannel.send(currentGuildVars.defensePlayer.name+ has been sentenced to death by the town.); }else{ currentGuildVars.mafiaChannel.send(currentGuildVars.defensePlayer.name+ has been voted inncocent by the town.); } if(checkWin()){ currentGuildVars.gameState=ENDGAME; currentGuildVars.mafiaChannel.send(All players will have chat functions now. 2 mins until the game deletes itself. Thanks for playing and remember to upvote in discords bots); currentGuildVars.logicGameTime=0 }else{ currentGuildVars.logicGameTime=DAYLENGTH; currentGuildVars.gameState=DAY; } } } if(currentGuildVars.gameState==ENDGAME) { currentGuildVars.mafiaChannel.overwritePermissions(server.id, { SEND_MESSAGES: true }); if(currentGuildVars.logicGameTime>=120){ resetVariables(); } } } currentGuildVars=tempcurrentGuildVars; },1000); } } //game has already started if (message.content.startsWith(prefix + 'play')&&(currentGuildVars.gameState!=-1&&currentGuildVars.gameState!=4)) { message.channel.send(The game has started. No more players are allowed to join. Please wait for the next round.); } if (message.content.startsWith(prefix + 'investigate')&&currentPlayer!=null&&currentPlayer.role == DETECTIVE&&currentGuildVars.gameState == NIGHT&&currentPlayer.alive) { var searchNum = parseInt(message.content.substring(prefix.length + 11)); if(!isNaN(searchNum) && searchNum >= 0 && searchNum <= currentGuildVars.playerList.length - 1) { if(currentGuildVars.playerList[searchNum]!= currentPlayer) { if(currentGuildVars.playerList[searchNum].alive) { name.send(You are going to investigate + currentGuildVars.playerList[searchNum].name + 's house.); currentPlayer.detectivePm = name; currentPlayer.searched = currentGuildVars.playerList[searchNum]; } else { name.send(You cannot investigate dead people.); } } else { name.send(You cannot investigate yourself.); } } else { name.send(No No No Please try again); } } if (message.content.startsWith(prefix + 'heal')&&currentPlayer!=null&&currentPlayer.role == DOCTOR&&currentGuildVars.gameState == NIGHT&&currentPlayer.alive) { var searchNum = parseInt(message.content.substring(prefix.length + 4)); if(!isNaN(searchNum) && searchNum >= 0 && searchNum <= currentGuildVars.playerList.length - 1) { if(currentGuildVars.playerList[searchNum].alive) { if(currentGuildVars.playerList[searchNum]!= currentPlayer.healedLastRound) { if(currentGuildVars.playerList[searchNum]!= currentPlayer) { name.send(You chose to save + currentGuildVars.playerList[searchNum].name); } else { name.send(You chose to save yourself. Ehhh so selfish~~); } currentPlayer.healed = currentGuildVars.playerList[searchNum]; } else { name.send(You can't pick the same person twice in a row.); } } else { name.send(You cannot heal dead people.); } } else { name.send(No No No Please try again); } } //the mafias have to vote to kill someone. if (message.content.startsWith(prefix + 'kill')&&currentPlayer!=null&&currentPlayer.role == MAFIA&&currentGuildVars.gameState == NIGHT&&currentPlayer.alive) { var searchNum = parseInt(message.content.substring(prefix.length + 4)); //grabs the player that someone inputted. if(!isNaN(searchNum) && searchNum >= 0 && searchNum <= currentGuildVars.playerList.length - 1) { if(currentGuildVars.playerList[searchNum].alive) { if(currentGuildVars.playerList[searchNum]!= currentPlayer) { if(currentGuildVars.playerList[searchNum].role!= MAFIA) { currentPlayer.kill = currentGuildVars.playerList[searchNum]; name.send(You voted to kill + currentGuildVars.playerList[searchNum].name + .); } else { name.send(You cannot vote to kill another mafia); } } else { name.send(Why would you want to kill yourself?); } } else { name.send(This person is already dead.); } } } //in the morning everyone votes to eliminiate someome. if(message.content.startsWith(prefix + 'vote')&&currentGuildVars.gameState == DAY&&currentPlayer!=null&&currentPlayer.alive) { var tempStr = message.content.split( ); if(!isNaN(tempStr[1]) && currentGuildVars.playerList[Number(tempStr[1])]!=null){ if(currentGuildVars.playerList[Number(tempStr[1])]==currentPlayer){ message.channel.send(You can not vote for yourself.); } if(!currentGuildVars.playerList[Number(tempStr[1])].alive){ message.channel.send(You can not vote for dead people.); } if(currentPlayer.hasVoted&&currentPlayer.votedWhat==currentGuildVars.playerList[Number(tempStr[1])].name){ message.channel.send(You have already voted for this player); } if(currentPlayer.hasVoted&&currentPlayer.votedWhat!=currentGuildVars.playerList[Number(tempStr[1])].name&&currentGuildVars.playerList[Number(tempStr[1])]!=currentPlayer){ for(var i in currentGuildVars.playerList){ if(currentGuildVars.playerList[i].name==currentPlayer.votedWhat){ currentGuildVars.playerList[i].voteCount--; } } message.channel.send(currentPlayer.name+ has retracted his vote aganist +currentPlayer.votedWhat); currentPlayer.hasVoted=false; currentPlayer.votedWhat=null; currentGuildVars.playerList[Number(tempStr[1])].voteCount+=1; message.channel.send(currentPlayer.name+ voted for +currentGuildVars.playerList[Number(tempStr[1])].name+ and they currently have +currentGuildVars.playerList[Number(tempStr[1])].voteCount+ votes aganist them.); currentPlayer.hasVoted=true; currentPlayer.votedWhat=currentGuildVars.playerList[Number(tempStr[1])].name; if(currentGuildVars.playerList[Number(tempStr[1])].voteCount>getAllAlivePlayers().length/2){ message.channel.send(currentGuildVars.playerList[Number(tempStr[1])].name+ has been voted on by the town and will be given 30 seconds to provide a defense before the town voted guilty or innocent.); currentGuildVars.gameState=DEFENSE; currentGuildVars.defensePlayer=currentGuildVars.playerList[Number(tempStr[1])]; currentGuildVars.logicGameTime=0; for(var i in currentGuildVars.playerList){ currentGuildVars.playerList[i].hasVoted=false; } } } if(currentGuildVars.playerList[Number(tempStr[1])].alive&&currentGuildVars.playerList[Number(tempStr[1])]!=currentPlayer&&!currentPlayer.hasVoted){ currentGuildVars.playerList[Number(tempStr[1])].voteCount+=1; message.channel.send(currentPlayer.name+ voted for +currentGuildVars.playerList[Number(tempStr[1])].name+ and they currently have +currentGuildVars.playerList[Number(tempStr[1])].voteCount+ votes aganist them.); currentPlayer.hasVoted=true; currentPlayer.votedWhat=currentGuildVars.playerList[Number(tempStr[1])].name; if(currentGuildVars.playerList[Number(tempStr[1])].voteCount>getAllAlivePlayers().length/2){ message.channel.send(currentGuildVars.playerList[Number(tempStr[1])].name+ has been voted on by the town and will be given 30 seconds to provide a defense before the town voted guilty or innocent.); currentGuildVars.gameState=DEFENSE; currentGuildVars.defensePlayer=currentGuildVars.playerList[Number(tempStr[1])]; currentGuildVars.logicGameTime=0; for(var i in currentGuildVars.playerList){ currentGuildVars.playerList[i].hasVoted=false; } } } }else if(message.content.match(/<@(d+)>/i)!=null){ if(findPlayerWithId(Number(message.content.match(/<@(d+)>/i)[1]))!=null){ var inputPlayer = findPlayerWithId(Number(message.content.match(/<@(d+)>/i)[1])); var tempStorePlayer=inputPlayer; if(inputPlayer==currentPlayer){ message.channel.send(You can not vote for yourself.); } if(!tempStorePlayer.alive){ message.channel.send(You can not vote for dead people.); } if(currentPlayer.hasVoted&&currentPlayer.votedWhat==tempStorePlayer.name){ message.channel.send(You have already voted for this player); } if(currentPlayer.hasVoted&&currentPlayer.votedWhat!=tempStorePlayer.name&&inputPlayer!=currentPlayer){ for(var i in currentGuildVars.playerList){ if(currentGuildVars.playerList[i].name==currentPlayer.votedWhat){ currentGuildVars.playerList[i].voteCount--; } } message.channel.send(currentPlayer.name+ has retracted his vote aganist +currentPlayer.votedWhat); currentPlayer.hasVoted=false; currentPlayer.votedWhat=null; tempStorePlayer.voteCount+=1; message.channel.send(currentPlayer.name+ voted for +tempStorePlayer.name+ and they currently have +tempStorePlayer.voteCount+ votes aganist them.); currentPlayer.hasVoted=true; currentPlayer.votedWhat=tempStorePlayer.name; if(tempStorePlayer.voteCount>getAllAlivePlayers().length/2){ message.channel.send(tempStorePlayer.name+ has been voted on by the town and will be given 30 seconds to provide a defense before the town voted guilty or innocent.); currentGuildVars.gameState=DEFENSE; currentGuildVars.defensePlayer=tempStorePlayer; currentGuildVars.logicGameTime=0; for(var i in currentGuildVars.playerList){ currentGuildVars.playerList[i].hasVoted=false; } } } if(tempStorePlayer.alive&&tempStorePlayer!=currentPlayer&&!currentPlayer.hasVoted){ tempStorePlayer.voteCount+=1; message.channel.send(currentPlayer.name+ voted for +tempStorePlayer.name+ and they currently have +tempStorePlayer.voteCount+ votes aganist them.); currentPlayer.hasVoted=true; currentPlayer.votedWhat=tempStorePlayer.name; if(tempStorePlayer.voteCount>getAllAlivePlayers().length/2){ message.channel.send(tempStorePlayer.name+ has been voted on by the town and will be given 30 seconds to provide a defense before the town voted guilty or innocent.); currentGuildVars.gameState=DEFENSE; currentGuildVars.defensePlayer=tempStorePlayer; currentGuildVars.logicGameTime=0; for(var i in currentGuildVars.playerList){ currentGuildVars.playerList[i].hasVoted=false; } } } } } } if(message.content.startsWith(prefix + 'guilty')&&currentGuildVars.gameState == VOTING&&currentPlayer!=null&&currentPlayer.alive) { if(currentPlayer.hasVoted&&currentPlayer.votedWhat){ message.channel.send(You have already voted. Please use m.votecancel to cancel your vote.); } if(currentPlayer.hasVoted&&!currentPlayer.votedWhat){ currentGuildVars.guiltCount-=2; currentPlayer.hasVoted=true; currentPlayer.votedWhat=false; message.channel.send(currentPlayer.name + has changed his mind and voted guilty.); } if(currentPlayer!=currentGuildVars.defensePlayer&&!currentPlayer.hasVoted){ currentGuildVars.guiltCount-=1; currentPlayer.hasVoted=true; currentPlayer.votedWhat=true; message.channel.send(currentPlayer.name + has voted guilty.); } } if(message.content.startsWith(prefix + 'innocent')&&currentGuildVars.gameState == VOTING&&currentPlayer!=null&&currentPlayer.alive) { if(currentPlayer.hasVoted&&!currentPlayer.votedWhat){ message.channel.send(You have already voted. Please use m.votecancel to cancel your vote.); } if(currentPlayer.hasVoted&&currentPlayer.votedWhat){ currentGuildVars.guiltCount+=2; currentPlayer.hasVoted=true; currentPlayer.votedWhat=true; message.channel.send(currentPlayer.name + has changed his mind and voted innocent.); } if(currentPlayer!=currentGuildVars.defensePlayer&&!currentPlayer.hasVoted){ currentGuildVars.guiltCount+=1; currentPlayer.hasVoted=true; currentPlayer.votedWhat=false; message.channel.send(currentPlayer.name + has voted innocent.); } } if(message.content.startsWith(prefix + 'cancelvote')&&(currentGuildVars.gameState == VOTING || currentGuildVars.gameState == DAY)&&currentPlayer!=null&&currentPlayer.alive&&currentPlayer.hasVoted) { //voting stage if(currentGuildVars.gameState == VOTING){ //guilty vote if(currentPlayer.votedWhat){ currentGuildVars.guiltCount+=1; currentPlayer.hasVoted=false; currentPlayer.votedWhat=null; message.channel.send(currentPlayer.name+ has retracted his guilty vote.); }else{ currentGuildVars.guiltCount-=1; currentPlayer.hasVoted=false; currentPlayer.votedWhat=null; message.channel.send(currentPlayer.name+ has retracted his innocent vote.); } //day vote stage }else{ for(var i in currentGuildVars.playerList){ if(currentGuildVars.playerList[i].name==currentPlayer.votedWhat){ currentGuildVars.playerList[i].voteCount--; } } message.channel.send(currentPlayer.name+ has retracted his vote aganist +currentPlayer.votedWhat); currentPlayer.hasVoted=false; currentPlayer.votedWhat=null; } } if(message.content.startsWith(prefix + 'debug')&&(message.author.id==process.env.BOTTOKEN)){ message.channel.send( gameState + currentGuildVars.gameState + mafia + getAllMafiaPlayersNames() + gameTime + currentGuildVars.gameTime + logicGameTime + currentGuildVars.logicGameTime+ guiltCount + currentGuildVars.guiltCount); } if(message.content.startsWith(prefix + role)) { } ///NEED TO TEST!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! if(message.content.startsWith(prefix + 'check')&&currentGuildVars.gameState!=NOGAME&&currentGuildVars.gameState!=GAMESTARTING&&currentPlayer!=null&&currentPlayer.alive) { var tempNames=; var aliveNames = ; var deadNames = ; var someoneIsDead = false; for(var i in currentGuildVars.playerList) { // if(!playerList[i].alive) // tempNames+=(dead); // tempNames+=i+. +playerList[i].name+n; if(currentGuildVars.playerList[i].alive) { aliveNames += i + . + currentGuildVars.playerList[i].name; //if the message was sent from a pm with the bot then tag on their role as well. if(message.channel.type == dm) { if(currentPlayer.role == MAFIA && currentGuildVars.playerList[i].role == MAFIA) aliveNames += (Mafia); else if(currentPlayer == currentGuildVars.playerList[i]) { if(currentPlayer.role == DOCTOR) aliveNames += (Doctor); if(currentPlayer.role == DETECTIVE) aliveNames += (Detective); if(currentPlayer.role == TOWN) aliveNames += (Town); } } aliveNames += n; } else { deadNames += i + . + currentGuildVars.playerList[i].name + n; someoneIsDead = true; } } if(someoneIsDead) tempNames += Alive: n + aliveNames + Dead: n + deadNames; else tempNames += aliveNames; message.channel.send(tempNames); } //testing db if(message.content.startsWith(prefix + 'insert')&& message.author.id==process.env.BOTTOKEN){ var doc = { hello: 'world' , n: 5 , today: new Date() , nedbIsAwesome: true , notthere: null , notToBeSaved: undefined // Will not be saved , fruits: [ 'apple', 'orange', 'pear' ] , infos: { name: 'nedb' } }; }); } if(message.content.startsWith(prefix + 'print')&& message.author.id==process.env.BOTTOKEN){ db.find({ hello: 'world' }, function (err, docs) { console.log(docs); console.log(docs[0].hello); }); } if(message.content.startsWith(prefix + 'testTimer')&& message.author.id==process.env.BOTTOKEN){ currentGuildVars.mafiaChannel=message.channel; currentGuildVars.testTimer = setInterval(function(){ currentGuildVars.gameTime++; currentGuildVars.mafiaChannel.send(THis is a test, plz ignore : +currentGuildVars.id.name+n Time= +currentGuildVars.gameTime); },5000); } //ignore this part if (message.content == good night || message.content == gn || message.content == gnight) { message.channel.send(Good night + name.username); console.log(message.channel); } if (message.content == good morning || message.content == gm || message.content == gmorning) { message.channel.send(Good morning + name.username); console.log(message.channel); } }); client.login(process.env.TOKEN); If a player cancels their guilty vote during the voting phase when the guiltCount is currently -2, what will the new guiltCount value be?

answer:Based on the code, when a player cancels their guilty vote during the voting phase: 1. It first checks if the player has already voted (currentPlayer.hasVoted) and that they previously voted guilty (currentPlayer.votedWhat is true). 2. If so, it will add 1 back to the guiltCount: ``` currentGuildVars.guiltCount+=1; ``` 3. So if the previous guiltCount was -2, and the player cancels their previous guilty vote, it will add 1 back, making the new guiltCount -1. Therefore, if the guiltCount is currently -2 and a player cancels their guilty vote, the new guiltCount value will be -1.

question:A farmer has 100 feet of fencing and wants to enclose a rectangular area that borders a river, with the river acting as a natural border on one side. What is the maximum area he can enclose using the fencing and how would you approach finding the dimensions of this area?

answer:To find the maximum area the farmer can enclose, we'll approach the problem by breaking it down into manageable parts. First, let's visualize the scenario: we have a rectangular area with one side being the river, which doesn't require fencing. This means the farmer will only need to use the fencing for three sides of the rectangle. Let's denote the width of the rectangle (parallel to the river) as 'w' and the length (perpendicular to the river) as 'l'. Since the farmer has 100 feet of fencing, we can create an equation to represent the total length of fencing used. The equation would be: 2l + w = 100. This equation accounts for the two lengths and one width of the rectangle that require fencing. We're looking to maximize the area, which is given by the formula A = l * w. To maximize this area, we'll need to express one variable in terms of the other using the equation we created for the fencing length. Let's solve for 'w' in terms of 'l': w = 100 - 2l. Substituting 'w' in the area formula, we get A = l * (100 - 2l). Expanding this equation, we have A = 100l - 2l^2. Now, we need to find the value of 'l' that maximizes the area 'A'. This is a quadratic equation, and the maximum value of a quadratic equation occurs at its vertex. We can find the vertex by using the formula l = -b / 2a. In our case, a = -2 and b = 100, so l = -100 / (2 * -2) = 100 / 4 = 25. Now that we know 'l' is 25, we can find 'w' using the equation w = 100 - 2l. Substituting 'l' into the equation, we get w = 100 - 2 * 25 = 50. The maximum area the farmer can enclose is A = l * w = 25 * 50 = 1250 square feet, and the dimensions of this area are 25 feet by 50 feet.

question:What is the significance of the derivative of area, A(l) = 100l - 2l^2? How does the derivative help with maximizing area?

answer:The derivative of the area function A(l) = 100l - 2l^2 represents the rate of change of the area with respect to the length 'l'. Taking the derivative of A(l) with respect to 'l', we get A'(l) = 100 - 4l. The significance of this derivative lies in its ability to help us find the critical points of the area function. Critical points are values of 'l' at which the area function changes from increasing to decreasing or vice versa. To maximize the area, we're interested in finding the critical point where the area function changes from increasing to decreasing. The derivative A'(l) = 100 - 4l tells us the rate at which the area is increasing or decreasing at any given value of 'l'. When the derivative is positive, the area is increasing, and when it's negative, the area is decreasing. To find the critical point, we set the derivative equal to zero and solve for 'l': A'(l) = 100 - 4l = 0. Solving for 'l', we get 4l = 100, and therefore l = 25. This critical point, l = 25, corresponds to the maximum area. The reason for this is that when l is less than 25, the derivative is positive, indicating that the area is increasing. When l is greater than 25, the derivative is negative, indicating that the area is decreasing. Therefore, the maximum area occurs at l = 25. The derivative helps us find the maximum area by identifying the point at which the area function changes from increasing to decreasing. By setting the derivative equal to zero and solving for 'l', we can efficiently find the critical point that corresponds to the maximum area. This approach is particularly useful when dealing with more complex functions, where finding the vertex of a quadratic equation might not be as straightforward.

question:The first derivative test states that a sign change in the first derivative from positive to negative indicates a relative maximum. How does the second derivative test work, and what does the sign of the second derivative indicate about a function's behavior around a critical point?

answer:The second derivative test is a method used to determine whether a critical point corresponds to a relative maximum, relative minimum, or neither. It involves analyzing the sign of the second derivative at the critical point. Given a function f(x), the first derivative f'(x) represents the rate of change of the function, as we discussed earlier. The second derivative f''(x) represents the rate of change of the first derivative. In other words, it measures the rate at which the rate of change is changing. The second derivative test states that: * If f''(x) > 0 at a critical point, then the function has a relative minimum at that point. This is because the second derivative indicates that the rate of change is increasing, which means the function is concave up. * If f''(x) < 0 at a critical point, then the function has a relative maximum at that point. This is because the second derivative indicates that the rate of change is decreasing, which means the function is concave down. * If f''(x) = 0 at a critical point, then the test is inconclusive, and further analysis is needed. In the context of the area function A(l) = 100l - 2l^2, we can find the second derivative by differentiating the first derivative A'(l) = 100 - 4l. This gives us A''(l) = -4. Since A''(l) = -4 is negative for all values of l, including l = 25, we can conclude that the area function has a relative maximum at l = 25. This confirms our previous result obtained using the first derivative test. The sign of the second derivative provides valuable information about the function's behavior around a critical point. A positive second derivative indicates that the function is concave up, while a negative second derivative indicates that the function is concave down. This information can be used to identify relative maxima and minima, as well as to understand the overall shape of the function.

Released under the Mit License.

has loaded