function Dice(matrixClient) { this.matrixClient = matrixClient; this.blacklistedRooms = []; } function getRandomInt(min, max) { max = Number(max) + 1 min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min)) + min; //The maximum and the minimum are inclusive } Dice.prototype.setBlacklistedRooms = function(blacklistedRooms) {this.blacklistedRooms = blacklistedRooms;}; Dice.prototype.handleMessage = function(event, room) { if (this.blacklistedRooms.indexOf(room.roomId) >=0) { return; } var messagePieces = event.getContent().body.split(" "); if (messagePieces[0] === "!roll") { // These default min and max values are so !roll give us a "chan-like" result var min = 100000000; var max = 1000000000; var numOfTimes = 1; switch (messagePieces.length) { case 4: numOfTimes = messagePieces[3] case 3: min = messagePieces[1]; max = messagePieces[2]; break; case 2: min = 1; max = messagePieces[1]; if (/^\d+d\d+$/.test(messagePieces[1])) { var dsplit = messagePieces[1].split("d"); numOfTimes = dsplit[0]; max = dsplit[1]; } case 1: break; default: this.matrixClient.sendNotice(room.roomId, "Invalid roll parameters."); return; } this.roll(min, max, numOfTimes, event, room); } } Dice.prototype.roll = function roll(min, max, numOfTimes, event, room) { if (isNaN(min) || isNaN(max) || isNaN(numOfTimes)) { this.matrixClient.sendNotice(room.roomId, "Invalid roll parameters."); return; } else if (numOfTimes > 10 || numOfTimes < 1) { this.matrixClient.sendNotice(room.roomId, "Minimum of 1 roll, maximum of 10 rolls."); return; } var results = new Array(numOfTimes); var formattedBody = ""+event.getSender()+": Your rolls are:";; var body = event.getSender()+ ": Your rolls are:" if (numOfTimes == 1) { var result = getRandomInt(min,max); formattedBody = ""+event.getSender()+": Your roll is: "+result; body = event.getSender()+ ": Your roll is: "+result; } else { for(var i = 0; i < numOfTimes; i++) { results[i] = getRandomInt(min,max); formattedBody += "
Roll "+(i+1)+": "+results[i]; body += "\r\nRoll "+(i+1)+": "+results[i]; } } var content = { "format": "org.matrix.custom.html", "body": body, "formatted_body": formattedBody, "msgtype": "m.text" }; this.matrixClient.sendEvent(room.roomId, "m.room.message", content, "", (err, res) => { if (err != null) { console.error(err); } }); } Dice.prototype.getFormattedHelp = function() { return "!roll - Roll a number from 100000000 to 1000000000
\ !roll [number of rolls]d[max] - Roll a number from 1 to [max] the number of times specified (10 max). Example: !roll 3d20
\ !roll [max] - Roll a number from 1 to [max] (example: !roll 100)
\ !roll [min] [max] - Roll a number from [min] to [max] (example: !roll 25 100)
\ !roll [min] [max] [number of rolls] - Roll a number from [min] to [max] the [number of rolls] specified (10 max) (example: !roll 25 100 5)" } Dice.prototype.getHelp = function() { return "!roll\" - Roll a number from 100000000 to 1000000000\r\n\ \"!roll [number of rolls]d[max]\" - Roll a number from 1 to [max] the number of times specified (10 max). Example: \"!roll 3d20\"\r\n\ \"!roll [max]\" - Roll a number from 1 to [max] (example: \"!roll 100\")\r\n\ \"!roll [min] [max]\" - Roll a number from [min] to [max] (example: \"!roll 25 100\")\r\n\ \"!roll [min] [max] [number of rolls]\" - Roll a number from [min] to [max] the [number of rolls] specified (10 max) (example: \"!roll 25 100 5\")" } module.exports = Dice;