forked from RnDAO/tc-botComm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (117 loc) · 3.84 KB
/
index.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const fs = require("node:fs");const path = require("node:path");
const {
fetchSettings,
updateGuild,
getRange,
} = require("./database/dbservice.js");
const { connectDB, removeConnection } = require("./database/connection");
const {
trackMessages,
updateChannelInfo,
updateAccountInfo,
} = require("./action/export.js");
const { Client, Intents } = require("discord.js");
require("dotenv").config();
const intents = new Intents(32767);
const client = new Client({ intents });
// login to discord
const discordLogin = async () => {
const eventsPath = path.join(__dirname, "events");
const eventFiles = fs
.readdirSync(eventsPath)
.filter((file) => file.endsWith(".js"));
client.once("ready", () => {
console.log(`Ready! Logged in as ${client.user.tag}`);
// once discord bot login then extract
extract();
});
await client.login(process.env.TOKEN);
};
/**
* @feature Track messages from discord between given time range
* @feature Insert messages rawinfo into database
* @feature Update account information
*/
const messageAction = async (setting) => {
const { guildId, selectedChannels, period } = setting;
try {
const channels = selectedChannels.map((item) => item.channelId);
const guild = await client.guilds.fetch(guildId);
const date = new Date(period);
const timeStamp = date.getTime();
const storedIdRange = await getRange(guild.id);
const [before, after] = [
storedIdRange[0]?.messageId,
storedIdRange[1]?.messageId,
];
const messages = await trackMessages(guild, null, "channels", {
channels: channels,
since: timeStamp,
before,
after,
});
return messages;
} catch (e) {
console.log(e);
return [];
}
};
// check the bot is connected to the guilds and update status
const checkBotStatus = async (settings) => {
const promises = settings.map(async (setting) => {
const { guildId } = setting;
const guild = client.guilds.cache.get(guildId);
if (!guild) {
setting.isDisconnected = true;
await updateGuild(guildId, setting);
}
});
return await Promise.all(promises);
};
// toggle bot status when extracting and after that
const toggleExtraction = async (setting, status) => {
const { guildId } = setting;
setting.isInProgress = status;
await updateGuild(guildId, setting);
};
// get guildid from command
const getGuildFromCmd = () => {
const args = process.argv.slice(2);
const portArg = args.find((arg) => arg.startsWith("--guild="));
const guild = portArg ? portArg.split("=")[1] : null;
return guild;
};
// extract messages from connected servers
const extract = async () => {
// only fetch connected guilds
const customGuildId = getGuildFromCmd();
const settings = await fetchSettings(customGuildId);
await checkBotStatus(settings);
console.info(settings.length, "connected discord servers");
for (const setting of settings) {
const { guildId, name } = setting;
console.info("start extraction from ", name);
console.info("make isProgress true in this server");
await toggleExtraction(setting, true);
console.info("sync channel id and channel name");
await updateChannelInfo(client, guildId);
console.info("sync account information");
await updateAccountInfo(client, guildId);
console.info("tracking messages from discord server ", name);
await messageAction(setting);
console.info("make isProgress false in this server");
await toggleExtraction(setting, false);
removeConnection(guildId);
}
process.exit(0);
};
/**
* extract messages from guild setting
* input: npm start -- --guild=853132782821703751 -> extract messages from only one guild(853132782821703751)
* npm start -> extract messages from all guilds
*/
const app = async () => {
await connectDB();
await discordLogin();
};
app();