2022-01-27 00:11:14 +01:00
|
|
|
class ApiClientUtil {
|
|
|
|
|
|
|
|
constructor(client) {
|
|
|
|
|
2022-07-24 10:46:51 +02:00
|
|
|
this.client = client;
|
2022-01-27 00:11:14 +01:00
|
|
|
this.cache = {};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-07-24 10:46:51 +02:00
|
|
|
get apiManager() {
|
|
|
|
return this.client.api;
|
|
|
|
}
|
|
|
|
|
2022-01-27 00:11:14 +01:00
|
|
|
async handleMessage(shard, message) {
|
|
|
|
|
|
|
|
const { type, id } = message;
|
|
|
|
let response = null;
|
2022-07-24 10:46:51 +02:00
|
|
|
|
2022-01-27 00:11:14 +01:00
|
|
|
try {
|
|
|
|
switch (type) {
|
|
|
|
case 'guild-live':
|
|
|
|
response = await this.getLiveGuild(message);
|
|
|
|
break;
|
|
|
|
case 'settings-index':
|
|
|
|
response = await this.settingsIndex(message);
|
|
|
|
break;
|
|
|
|
case 'user':
|
|
|
|
response = await this.user(message);
|
|
|
|
break;
|
|
|
|
case 'settings':
|
|
|
|
response = await this.settings(message);
|
|
|
|
break;
|
2022-07-24 10:46:51 +02:00
|
|
|
case 'restart':
|
|
|
|
response = await this.restartAPIShards(message);
|
|
|
|
break;
|
2022-01-27 00:11:14 +01:00
|
|
|
default:
|
|
|
|
return shard.send({ id, failure: true, error: '[IPC] No such type' });
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
this.client.logger.error(`[CliUtil] ${error.stack || error}`);
|
|
|
|
return shard.send({ id, failure: true, error });
|
|
|
|
}
|
|
|
|
|
|
|
|
this.client.logger.debug(`Result: ${JSON.stringify(response)}`);
|
|
|
|
shard.send({ id, data: response, failure: !response });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async settings({ guildId }) {
|
|
|
|
|
|
|
|
const evalFunc = async (client, { guildId }) => {
|
2022-07-24 10:46:51 +02:00
|
|
|
const wrapper = client.getGuildWrapper(guildId);
|
2022-01-27 00:11:14 +01:00
|
|
|
const settings = await wrapper.settings();
|
|
|
|
return settings;
|
|
|
|
};
|
|
|
|
|
|
|
|
const results = await this.client.shardingManager.broadcastEval(evalFunc, { context: { guildId } });
|
|
|
|
|
|
|
|
return results.find((result) => result !== null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async user({ userid }) {
|
|
|
|
|
|
|
|
const evalFunc = async (client, { user }) => {
|
|
|
|
const _user = await client.users.fetch(user);
|
|
|
|
return _user.toJSON();
|
|
|
|
};
|
|
|
|
const _shard = this.client.shardingManager.shards.random();
|
|
|
|
const user = await _shard.eval(evalFunc, { user: userid });
|
|
|
|
return user;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async settingsIndex() {
|
|
|
|
|
|
|
|
if (this.cache['settings-index']?.last < Date.now() - 3600_000) return this.cache['settings-index'].value;
|
|
|
|
const evalFunc = (client) => {
|
|
|
|
const result = client.registry.settings.reduce((acc, setting) => {
|
|
|
|
if (!acc[setting.module.name]) acc[setting.module.name] = [];
|
|
|
|
acc[setting.module.name].push({ name: setting.name, ...setting.definitions });
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
const _shard = this.client.shardingManager.shards.random();
|
|
|
|
const index = await _shard.eval(evalFunc);
|
|
|
|
this.cache['settings-index'] = { value: index, last: Date.now() };
|
|
|
|
return index;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async getLiveGuild(message) { //eslint-disable-line no-unused-vars
|
|
|
|
|
|
|
|
const { guildId } = message;
|
|
|
|
const evalFunc = (client, { guildId }) => {
|
|
|
|
const guild = client.guilds.cache.get(guildId);
|
|
|
|
if (!guild) return null;
|
|
|
|
const wrapper = new client.wrapperClasses.GuildWrapper(client, guild);
|
|
|
|
return wrapper.toJSON();
|
|
|
|
};
|
|
|
|
|
|
|
|
this.client.logger.debug(`guild-live request - shard: ${message.shard}, message id ${message.id}`);
|
|
|
|
const result = await this.client.shardingManager.broadcastEval(evalFunc, { context: { guildId } });
|
|
|
|
const guild = result.find((elem) => elem !== undefined);
|
|
|
|
return guild;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-07-24 10:46:51 +02:00
|
|
|
async restartAPIShards(message) {
|
|
|
|
const { shards } = message;
|
|
|
|
await this.apiManager.restartShards(shards);
|
|
|
|
}
|
|
|
|
|
2022-01-27 00:11:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ApiClientUtil;
|