Skip to content

Commit 61e48f3

Browse files
committed
Add initial set of plugins
message_output: Log all messages the bot receives to the console request_q: A simple queue for requests. Can be used as to-do list roll_dice: Plugin to roll an arbitary amount of dice
1 parent 7366088 commit 61e48f3

4 files changed

Lines changed: 190 additions & 0 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,6 @@ tenc
223223

224224
# Plugins Folder
225225
plugins/*
226+
!plugins/roll_dice.dbot.js
227+
!plugins/message_output.dbot.js
228+
!plugins/request_q.dbot.js

plugins/message_output.dbot.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var api;
2+
3+
function chatmsg(data) {
4+
console.log(data.username + " (" + data.userID + ") in [" + data.channelID + "]: " + data.message);
5+
}
6+
7+
module.exports = {
8+
meta_inf: {
9+
name: "Message Output",
10+
version: "1.0.0",
11+
description: "Read the chat from the bot console.",
12+
author: "Wolvan"
13+
},
14+
load: function (_api) {
15+
api = _api;
16+
},
17+
start: function () {
18+
api.Events.on("message", chatmsg);
19+
},
20+
stop: function () {
21+
api.Events.removeListener("message", chatmsg);
22+
}
23+
}

plugins/request_q.dbot.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
var api;
2+
var storage;
3+
4+
function addRequestToQEvent(channelID, request) {
5+
var requests = storage.getItem(channelID + "_requests") || [];
6+
requests.push(request);
7+
storage.setItem(channelID + "_requests", requests);
8+
api.Messages.send(channelID, "Added request '" + request + "'");
9+
}
10+
function handleCmd(data) {
11+
var args = data.args;
12+
if (!args[0] || args[0].toLowerCase() === "list") {
13+
var requests = storage.getItem(data.channelID + "_requests") || [];
14+
var msg = "There currently " + (requests.length !== 1 ? "are" : "is") + " " + (!requests.length ? "no" : requests.length) + " request" + (requests.length !== 1 ? "s" : "") + " in the queue" + (requests.length ? ":" : ".");
15+
requests.forEach(function (item, index) {
16+
msg = msg + "\n" + (index + 1) + " - " + item;
17+
});
18+
api.Messages.send(data.channelID, msg);
19+
} else if (args[0] === "?" || args[0].toLowerCase() === "help") {
20+
api.Messages.send(data.channelID, "Add, delete or list requests! You can also raffle a random request!");
21+
} else if (args[0].toLowerCase() === "raffle") {
22+
var requests = storage.getItem(data.channelID + "_requests") || [];
23+
if (!requests.length) {
24+
api.Messages.send(data.channelID, "No requests to raffle");
25+
return;
26+
}
27+
api.Messages.send(data.channelID, "Do this request: " + requests[Math.floor(Math.random() * requests.length)]);
28+
} else if (args[0].toLowerCase() === "delete" || args[0].toLowerCase() === "del" || args[0].toLowerCase() === "rm") {
29+
var index = parseInt(args[1]) - 1;
30+
if (isNaN(index)) {
31+
api.Messages.send(data.channelID, "No index to remove specified.");
32+
return;
33+
}
34+
var requests = storage.getItem(data.channelID + "_requests") || [];
35+
var removed = requests.splice(index, 1)[0];
36+
storage.setItem(data.channelID + "_requests", requests);
37+
removed ? api.Messages.send(data.channelID, "Removed request '" + removed + "'") : api.Messages.send(data.channelID, "Request with index " + (index + 1) + " not found.");
38+
} else if (parseInt(args[0])) {
39+
var index = parseInt(args[0]) - 1;
40+
var requests = storage.getItem(data.channelID + "_requests") || [];
41+
requests[index] ? api.Messages.send(data.channelID, (index + 1) + " - " + requests[index]) : api.Messages.send(data.channelID, "Request with index " + (index + 1) + " not found.");
42+
} else {
43+
if (args[0].toLowerCase() === "add") args.splice(0, 1);
44+
var requests = storage.getItem(data.channelID + "_requests") || [];
45+
requests.push(args.join(" "));
46+
storage.setItem(data.channelID + "_requests", requests);
47+
api.Messages.send(data.channelID, "Added request '" + args.join(" ") + "'");
48+
}
49+
}
50+
module.exports = {
51+
meta_inf: {
52+
name: "Request Queue",
53+
version: "1.0.0",
54+
description: "Store requests for later.",
55+
author: "Wolvan"
56+
},
57+
load: function (_api, _storage) {
58+
api = _api;
59+
storage = _storage;
60+
api.Events.on("requestq_addRequestToQ", addRequestToQEvent);
61+
},
62+
start: function () {
63+
api.Events.on("chatCmd#request", handleCmd);
64+
api.Events.on("chatCmd#requests", handleCmd);
65+
},
66+
stop: function () {
67+
api.Events.removeListener("chatCmd#request", handleCmd);
68+
api.Events.removeListener("chatCmd#requests", handleCmd);
69+
},
70+
unload: function () {
71+
api.Events.removeListener("requestq_addRequestToQ", addRequestToQEvent);
72+
}
73+
}

plugins/roll_dice.dbot.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
var api;
2+
function handleCommands(data) {
3+
if (data.cmd === "roll") {
4+
var dieString = data.args[0];
5+
if (!dieString) dieString = "[1d20]";
6+
dieString = dieString.replace("[", "").replace("]", "");
7+
8+
if (dieString === "?") {
9+
api.Messages.send("Roll dice! Format: [xdy+/-xdy+/-xdy+/-xdy+/-...+/-z]. Max x: 200, Max y: 200");
10+
return;
11+
}
12+
13+
var dicesplit1 = dieString.replace(/-/g, ",-").split("+");
14+
var dice = [];
15+
dicesplit1.forEach(function (item) {
16+
item.split(",").forEach(function (item2) {
17+
dice.push(item2);
18+
});
19+
})
20+
var rolls = [];
21+
var times = 0;
22+
var die = 0;
23+
var i = 0;
24+
var isNegative = false;
25+
var roll = 0;
26+
var modifiers = [];
27+
var dieTooHigh = false;
28+
29+
dice.forEach(function (item) {
30+
if (dieTooHigh) return;
31+
if (!item) return;
32+
if (item.indexOf("d") !== -1) {
33+
isNegative = false;
34+
if (item.indexOf("-") !== -1) {
35+
isNegative = true;
36+
item = item.replace(/-/g, "");
37+
}
38+
times = parseInt(item.split("d")[0]);
39+
die = parseInt(item.split("d")[1]);
40+
if (times > 200 || die > 200) {
41+
dieTooHigh = true;
42+
return;
43+
}
44+
for (i = 0; i < times; i++) {
45+
roll = Math.floor((Math.random() * die) + 1);
46+
roll = isNegative ? -roll : roll;
47+
rolls.push(roll);
48+
}
49+
} else {
50+
modifiers.push(parseInt(item));
51+
}
52+
});
53+
54+
if (dieTooHigh) {
55+
api.Messages.send(data.channelID, "One or more of the dice you tried to throw are too high! Max is 200d200.");
56+
return;
57+
}
58+
59+
var sum = 0;
60+
rolls.forEach(function (item) { sum += item; });
61+
modifiers.forEach(function (modifier) {
62+
sum += modifier;
63+
});
64+
var NaNCount = 0;
65+
rolls.forEach(function(item) {if (isNaN(item)) NaNCount++;});
66+
modifiers.forEach(function(item) {if (isNaN(item)) NaNCount++;});
67+
var msg = " { " + rolls.join(" + ") + " }" + (modifiers.length ? (" + " + modifiers.join(" + ")).replace(/\+ -/g, "- ") : "") + " = " + sum;
68+
api.Messages.send(data.channelID, msg);
69+
if (NaNCount >= 16) {
70+
setTimeout(api.Messages.send.bind(this, data.channelID, "Batman!"), 4000);
71+
}
72+
}
73+
}
74+
75+
module.exports = {
76+
meta_inf: {
77+
name: "Roll the dice",
78+
version: "1.1.0",
79+
description: "A simple dice rolling plugin.",
80+
author: "Wolvan"
81+
},
82+
load: function (_api) {
83+
api = _api;
84+
},
85+
start: function () {
86+
api.Events.on("chatCmd", handleCommands);
87+
},
88+
stop: function () {
89+
api.Events.removeListener("chatCmd", handleCommands);
90+
}
91+
}

0 commit comments

Comments
 (0)