matrix-dice/index.js

116 lines
4.7 KiB
JavaScript
Raw Permalink Normal View History

2022-10-17 00:05:46 -04:00
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 = "<a href=\"https://matrix.to/#/"+event.getSender() + "\">"+event.getSender()+"</a>: Your rolls are:";;
var body = event.getSender()+ ": Your rolls are:"
if (numOfTimes == 1) {
var result = getRandomInt(min,max);
formattedBody = "<a href=\"https://matrix.to/#/"+event.getSender() + "\">"+event.getSender()+"</a>: 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 += "<br />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 "<code>!roll</code> - Roll a number from 100000000 to 1000000000<br />\
<code>!roll [number of rolls]d[max]</code> - Roll a number from 1 to [max] the number of times specified (10 max). Example: <code>!roll 3d20</code><br />\
<code>!roll [max]</code> - Roll a number from 1 to [max] (example: <code>!roll 100</code>)<br />\
<code>!roll [min] [max]</code> - Roll a number from [min] to [max] (example: <code>!roll 25 100</code>)<br />\
<code>!roll [min] [max] [number of rolls]</code> - Roll a number from [min] to [max] the [number of rolls] specified (10 max) (example: <code>!roll 25 100 5</code>)"
}
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;