forked from black-river-gaming/albion-killbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.js
100 lines (92 loc) · 2.66 KB
/
config.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
const logger = require("./logger")("config");
const database = require("./database");
const { hasSubscription } = require("./subscriptions");
exports.categories = ["general", "events", "battles", "rankings"];
const SERVER_CONFIG_COLLECTION = "guildConfig";
const DEFAULT_CONFIG = {
trackedPlayers: [],
trackedGuilds: [],
trackedAlliances: [],
dailyRanking: "daily",
categories: {
general: true,
events: true,
battles: true,
rankings: true,
},
mode: "image",
lang: "en",
};
exports.getConfig = async (guild) => {
const collection = database.collection(SERVER_CONFIG_COLLECTION);
if (!collection) {
return DEFAULT_CONFIG;
}
try {
return await collection.findOne({ guild: guild.id });
} catch (e) {
logger.error(`Unable to find guildConfig for guild ${guild}: ${e}`);
return null;
}
};
exports.getConfigBySubscription = async (userId) => {
const collection = database.collection(SERVER_CONFIG_COLLECTION);
if (!collection) {
return null;
}
try {
return await collection.findOne({ "subscription.owner": userId });
} catch (e) {
logger.error(`Unable to find guildConfig for discord user ${userId}: ${e}`);
return null;
}
};
exports.getConfigByGuild = async (guildList) => {
const configByGuild = {};
guildList.forEach((guild) => {
configByGuild[guild.id] = DEFAULT_CONFIG;
});
const collection = database.collection(SERVER_CONFIG_COLLECTION);
if (!collection) return configByGuild;
try {
const results = await collection.find({}).toArray();
results.forEach((config) => {
if (!hasSubscription(config)) return;
if (configByGuild[config.guild]) {
configByGuild[config.guild] = config;
}
});
return configByGuild;
} catch (e) {
logger.error(`Unable to find guildConfig for ${guildList.length} guilds: ${e}`);
return configByGuild;
}
};
exports.setConfig = async (guild) => {
const collection = database.collection(SERVER_CONFIG_COLLECTION);
if (!collection) {
return false;
}
try {
if (!guild.config) {
guild.config = DEFAULT_CONFIG;
}
guild.config.name = guild.name;
const guildConfig = await collection.updateOne({ guild: guild.id }, { $set: guild.config }, { upsert: true });
return guildConfig;
} catch (e) {
logger.error(`Unable to write guildConfig for guild ${guild}: ${e}`);
return false;
}
};
exports.deleteConfig = async (guild) => {
const collection = database.collection(SERVER_CONFIG_COLLECTION);
if (!collection) {
return false;
}
try {
return await collection.deleteOne({ guild: guild.id });
} catch (e) {
logger.error(`Unable to delete guildConfig for guild ${guild}: ${e}`);
}
};