Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
121 lines (107 loc) · 3.25 KB
/
Copy pathindex.js
File metadata and controls
121 lines (107 loc) · 3.25 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
111
112
113
114
115
116
117
118
119
120
121
const simpleGit = require("simple-git");
const log4js = require("log4js");
const Sequelize = require("sequelize");
const { Client, GatewayIntentBits, ActivityType } = require("discord.js");
const { Octokit } = require("@octokit/rest");
const { createDbSchema, createCacheDbSchema } = require("./others/dbSchema.js");
const { parseUsers } = require("./others/gitUtils.js");
const {
clientInit,
sequelizeInit,
checkGithubPermissions,
} = require("./startUtils.js");
const dotenv = require("dotenv");
dotenv.config({ quiet: true });
// Logger
log4js.configure("./log4js.json");
const logger = log4js.getLogger();
const sqlLogger = log4js.getLogger("sql");
const errorLogger = log4js.getLogger("error");
// Error logging
process.on("uncaughtException", (err) => {
errorLogger.error("Uncaught Exception:", err);
});
process.on("unhandledRejection", (reason, promise) => {
errorLogger.error("Unhandled Rejection at:", promise, "reason:", reason);
});
// Create a new client instance
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildPresences,
],
});
// Establish DB connection
const sequelize = new Sequelize({
dialect: "sqlite",
logging: (msg) => sqlLogger.debug(msg),
storage: "./data/database.sqlite",
define: {
freezeTableName: true
}
});
const sequelize_cache = new Sequelize({
dialect: "sqlite",
logging: (msg) => sqlLogger.debug(msg),
storage: "./data/cache.sqlite",
define: {
freezeTableName: true
}
});
// Establish Github connection
const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN });
// Git repo setup
const git = simpleGit();
// Create tables models
const db = createDbSchema(sequelize);
const cache = createCacheDbSchema(sequelize_cache);
module.exports = { db, cache, octokit, client, sequelize, git };
async function start() {
logger.info("-".repeat(40));
logger.info("Layout List Bot starting...");
logger.info("-".repeat(40));
try {
await sequelizeInit(db, cache);
} catch (error) {
logger.error("Unable to sync database data: \n", error);
process.exit(1);
}
try {
await clientInit(client);
} catch (error) {
logger.error("Unable to initialize client: \n", error);
process.exit(1);
}
await checkGithubPermissions(octokit);
try {
logger.info("Logging in client with discord...");
await client.login(process.env.TOKEN);
logger.info(`Client logged in as ${client.user.username}`);
} catch (error) {
logger.error("Unable to login client: \n", error);
process.exit(1);
}
try {
logger.info("Updating cache...");
let result = await parseUsers();
if (typeof result === "string") throw new Error(result);
if (result === 404) logger.info("No new users.");
} catch (e) {
logger.error(e);
}
try {
client.user.setPresence({
activities: [
{
name: "layouts",
type: ActivityType.Playing,
},
],
status: "online",
});
} catch (e) {
logger.error(`Error setting presence: ${e}`);
}
}
start();