-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
110 lines (93 loc) · 3.29 KB
/
app.js
File metadata and controls
110 lines (93 loc) · 3.29 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
require('dotenv').config();
const {App, ExpressReceiver} = require('@slack/bolt');
const {getStatsMessage, getHelpMessage} = require('./utils');
const statsManager = require('./statsManager');
const {processMessage} = require('./gameLogic');
const AsyncLock = require('async-lock');
// Create a custom receiver
const receiver = new ExpressReceiver({
signingSecret: process.env.SLACK_SIGNING_SECRET,
processBeforeResponse: true
});
// Create the Bolt app, using the custom receiver
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
receiver
});
// Handle the challenge request
receiver.router.post('/', (req, res) => {
if (req.body.type === 'url_verification') {
res.send(req.body.challenge);
} else {
res.sendStatus(404);
}
});
const lock = new AsyncLock();
const messageQueue = [];
// Function to process messages (both regular and !eval)
async function processAndRespond(message, say, client, isEval = false) {
try {
const result = await processMessage(message, say, client, isEval);
if (isEval) {
// For !eval, we want to return the result explicitly
if (result) {
await say(result);
} else {
await say("Invalid expression or operation not allowed.");
}
}
} catch (error) {
console.error('Error processing message:', error);
if (isEval) {
await say("An error occurred while processing the expression.");
}
}
}
app.message(/^(?!!)[^!].*$/, async ({message, say, client}) => {
if (message.channel !== process.env.COUNTING_GAME_CHANNEL_ID) return;
messageQueue.push({message, say, client});
// Process the queue
lock.acquire('messageProcessing', async (done) => {
while (messageQueue.length > 0) {
const {message, say, client} = messageQueue.shift();
await processAndRespond(message, say, client);
}
done();
}, (err, ret) => {
if (err) {
console.error('Error processing message queue:', err);
}
});
});
// Add the !eval command
app.message(/^!eval (.+)$/, async ({message, say, client, context}) => {
if (message.channel !== process.env.COUNTING_GAME_CHANNEL_ID) return;
const evalExpression = context.matches[1].trim();
const evalMessage = {...message, text: evalExpression};
await processAndRespond(evalMessage, say, client, true);
});
app.command('/counting-stats', async ({command, ack, say, client}) => {
await ack();
const statsMessage = await getStatsMessage(client, statsManager.getStats());
await say(statsMessage);
});
app.command('/counting-help', async ({command, ack, say}) => {
await ack();
await say(getHelpMessage());
});
app.message('!help', async ({message, say}) => {
if (message.channel === process.env.COUNTING_GAME_CHANNEL_ID) {
await say(getHelpMessage());
}
});
app.message('!stats', async ({message, say, client}) => {
if (message.channel === process.env.COUNTING_GAME_CHANNEL_ID) {
const statsMessage = await getStatsMessage(client, statsManager.getStats());
await say(statsMessage);
}
});
(async () => {
await statsManager.loadStats();
await app.start(process.env.PORT || 3000);
console.log('⚡️ Counting game bot is running!');
})();