-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetUserData.ts
88 lines (71 loc) · 2 KB
/
getUserData.ts
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
import { App } from "@slack/bolt";
import { Database } from "bun:sqlite";
const slackClient = new App({
signingSecret: process.env.SLACK_SIGNING_SECRET,
token: process.env.SLACK_BOT_TOKEN,
}).client;
const start = confirm("are you sure that you want to reindex the slack users?");
if (!start) {
console.log("Exiting...");
process.exit(0);
}
// create / init sqlite db
const db = new Database("users.db");
type User = {
id: string;
name: string;
real_name: string;
is_admin: boolean;
};
const assumedMemberCount = 62849;
function rePrint(text: string) {
process.stdout.clearLine(0);
process.stdout.cursorTo(0);
process.stdout.write(text);
}
// async inline
(async () => {
try {
// create table if not exists
db.exec(
"CREATE TABLE IF NOT EXISTS users (id TEXT PRIMARY KEY, name TEXT, real_name TEXT, is_admin BOOLEAN)"
);
const stmt = db.prepare("INSERT OR REPLACE INTO users VALUES (?, ?, ?, ?)");
// get users
let memberCount = (
db.prepare("SELECT COUNT(*) as count FROM users").get() as {
count: number;
}
).count;
console.log(`Found ${memberCount} members in the db\n`);
// get users from slack
let nextCursor;
do {
const response = await slackClient.users.list({
cursor: nextCursor,
});
const members = response.members!;
let i = 0;
for (const member of members) {
rePrint(
`${i}/${members.length} ${
memberCount + i
}/${assumedMemberCount} inserting ${member.name}`
);
const asd = member.profile?.display_name;
stmt.run(
member.id!,
member.profile?.display_name!,
member.profile?.real_name!,
member.is_admin!
);
i++;
}
memberCount += members.length;
nextCursor = response.response_metadata?.next_cursor;
} while (nextCursor);
console.log(`\n\nInserted ${memberCount} members into the db`);
} catch (error) {
console.error(error);
}
})();