Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
113 lines (77 loc) · 2.6 KB
/
Copy pathindex.js
File metadata and controls
113 lines (77 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
require("dotenv").config();
const { App } = require("@slack/bolt");
const fs = require("fs");
if (!fs.existsSync("people.json")) {
fs.writeFileSync("people.json", "[]")
}
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
appToken: process.env.SLACK_APP_TOKEN,
socketMode: true
});
app.command("/randombot-help", async ({ ack, respond }) => {
await ack();
await respond({
text:
`Available Commands:
/randombot-add [name] - It adds a new person to the list
/randombot-help - Return supported commands
`
});
});
app.command("/randombot-add", async ({ command, ack, respond }) => {
await ack();
const name = command.text.trim();
const people = JSON.parse(fs.readFileSync("people.json"));
if (name.length === 0) {
await respond("The name can not be empty!");
return;
}
if (people.includes(name)) {
await respond(`The name ${name} is already in the list!`);
return;
}
people.push(name);
fs.writeFileSync("people.json", JSON.stringify(people, null, 2));
await respond(`Added ${name} to the list!`);
});
app.command("/randombot-list", async ({ ack, respond }) => {
await ack();
const people = JSON.parse(fs.readFileSync("people.json"));
if (people.length === 0) {
await respond("The list is empty!");
return;
}
const list = people
.map((person, index) => `${index + 1}. ${person}`)
.join("\n");
await respond(`People in the list:\n\n${list}\n\nTotal: ${people.length} people`);
});
app.command("/randombot-pick", async ({ ack, respond }) => {
await ack();
const people = JSON.parse(fs.readFileSync("people.json"));
if (people.length === 0) {
await respond("The list is empty!");
return;
}
const randomPerson = people[Math.floor(Math.random() * people.length)];
await respond(`I picked: ${randomPerson}`);
});
app.command("/randombot-clear", async ({ ack, respond}) => {
await ack();
fs.writeFileSync("people.json", "[]");
await respond("The list has cleared succesfully!");
});
app.command("/randombot-info", async ({ ack, respond}) => {
await ack();
await respond(`
RandomBot
RandomBot is a simple Slack bot that manages a list of people and can randomly pick someone from it.
You can add people, view the list, randomly pick a person, clear the list, and see all available commands directly from Slack.
The bot is built with JavaScript, Node.js, Slack Bolt and Socket Mode. The list is saved locally in a JSON file so it can stay between restarts.
`);
});
(async () => {
await app.start();
console.log("bot is running!");
})();