-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
73 lines (56 loc) · 1.7 KB
/
main.js
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
const { WebClient } = require('@slack/web-api');
const token = process.env.SLACK_TOKEN;
const web = new WebClient(token);
const FETCH_SIZE = 200;
const arguments = process.argv;
if (arguments.length != 4) {
console.log('store slack token in env variable "SLACK_TOKEN" and call like this: ./node index <userId> <channelId>');
return 0;
}
const userId = arguments[2];
const channelId = arguments[3];
const deleteMessage = async (channel, ts) => {
const res = await web.chat.delete({
channel,
ts,
as_user: true
});
return res;
};
const getMessages = async (channel, cursor = null, limit = FETCH_SIZE) => {
const res = await web.conversations.history({
cursor,
channel,
limit
}).catch((reason) => {
console.error(`Channel ${channelId} not found`);
process.exit(0);
});
const { messages } = res;
const next_cursor = res.response_metadata.next_cursor;
return {
messages,
next_cursor
}
};
(async () => {
let next_cursor = null;
let messages = [];
while (true) {
let res = await getMessages(channelId, next_cursor);
messages = res.messages;
next_cursor = res.next_cursor;
console.log(`Fetched ${messages.length} comments`);
for (const message of messages) {
if (message.user === userId) {
const res = await deleteMessage(channelId, message.ts);
if (res.ok)
console.log(`Deleted: ${message.text}`);
else
console.error(`Deletion failed: ${message.text}`);
}
}
if (messages.length < FETCH_SIZE)
break;
}
})();