This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
104 lines (85 loc) · 3.17 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
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
/*
created by huda0209
EntryReSender for discord bot
main.js
ran by node.js
2022-9-23
*/
'use strict'
//node.js modules
const express = require("express");
const bodyParser = require('body-parser');
const discord = require("discord.js");
const EventEmitter = require("events");
require('date-utils');
//other
const DiscordClient = new discord.Client({intents: ["GUILDS", "GUILD_MESSAGES"], partials: ["USER", "MESSAGE", "CHANNEL"]});
const logger = require('./src/util/logFile');
const configManager = require("./src/config/configManager");
const Package = require("./package.json");
const apiMiddleware = require("./src/listener/httpRequest");
const tokenManager = require("./src/authorization/tokenManager");
const sabwebapiAuth = require("./src/callApi/sabwebapiAuthentication");
const sab = require("./src/callApi/sabwebapiCaller")
//init
require("./src/entry/entryContentManager").init();
require("./src/callApi/discordjsApiCaller").init(DiscordClient);
require("./src/config/RecruitmentsManager").init();
const apiSrv = express();
const entryEvent = new EventEmitter();
require("./src/listener/httpRequest").init(entryEvent);
require("./src/listener/receiveEntryListener")(entryEvent);
apiSrv.use(bodyParser.json({extended: true}));
apiSrv.use('/', apiMiddleware.middleware);
logger.info(`This service is standing now...`);
process.on("exit", ()=>{
sabwebapiAuth.invalidateToken();
configManager.saveConfig();
DiscordClient.destroy();
logger.info(`service end.`);
logger.hasLastLog();
console.log("Exitting...");
});
process.on("SIGINT", ()=>{
process.exit(0);
});
//start the discord bot
DiscordClient.on("ready", () => {
logger.info(`bot is ready! ver. ${Package.version} \n login: {cyan}${DiscordClient.user.tag}{reset}\n`);
DiscordClient.user.setActivity(`ver. ${Package.version}`, { type: 'PLAYING' });
});
//start the http api server
apiSrv.listen(configManager.getBotData("PORT"), ()=>{
logger.info(`http api server is ready!${configManager.getBotData("PORT")}\n start up : {green}${(new Date()).toFormat('DDD MMM DD YYYY HH24:MI:SS')}{reset}\n`);
});
let token;
if(process.argv.length == 3){
switch(process.argv[2]){
case "main" :
token = configManager.getBotData("MAIN_TOKEN");
break;
case "dev" :
if(!configManager.getBotData("DEV_TOKEN")){
logger.error(`Don't have a property "{red}DIV_TOKEN{reset}" in {green}setting.json{reset}.`);
process.exit(0);
}
token = configManager.getBotData("DEV_TOKEN");
require("./src/util/checkVersion");
break;
default :
logger.error(`Unknown command. \nUsage \n {green}node main.js main{reset} : use main token \n {green}node main.js div{reset} : use divelopment token`);
process.exit(0);
};
}else if(process.argv.length == 2){
token = configManager.getBotData("MAIN_TOKEN");
}else{
logger.error(`Unknown command. \nUsage \n {green}node main.js main{reset} : use main token \n {green}node main.js div{reset} : use divelopment token`);
process.exit(0);
}
DiscordClient.login(token)
.then(res=>{
logger.info(`Succeed to login the discord service.`);
})
.catch(error=>{
logger.error(`Could not login the discord service.\n${error}`);
});