galactic-bot/src/middleware/ApiClientUtil.ts

127 lines
3.8 KiB
TypeScript
Raw Normal View History

2023-06-22 01:07:19 +02:00
import { IPCMessage } from "../@types/Shared";
import BaseClient from "./Controller";
2022-01-27 00:11:14 +01:00
class ApiClientUtil {
2023-06-22 01:07:19 +02:00
#client: BaseClient;
#cache: {};
constructor (client: BaseClient) {
2022-01-27 00:11:14 +01:00
2023-06-22 01:07:19 +02:00
this.#client = client;
this.#cache = {};
2022-01-27 00:11:14 +01:00
}
2023-06-22 01:07:19 +02:00
get apiManager () {
return this.#client.api;
2022-07-24 10:46:51 +02:00
}
2023-06-22 01:07:19 +02:00
async handleMessage (shard: APIShard, message: IPCMessage) {
2022-01-27 00:11:14 +01:00
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) {
2023-06-22 01:07:19 +02:00
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;
case 'restart':
response = await this.restartAPIShards(message);
break;
default:
return shard.send({ id, failure: true, error: '[IPC] No such type' });
2022-01-27 00:11:14 +01:00
}
} catch (error) {
2023-06-22 01:07:19 +02:00
this.#client.logger.error(`[CliUtil] ${error.stack || error}`);
2022-01-27 00:11:14 +01:00
return shard.send({ id, failure: true, error });
}
this.client.logger.debug(`Result: ${JSON.stringify(response)}`);
shard.send({ id, data: response, failure: !response });
}
2023-06-22 01:07:19 +02:00
async settings ({ guildId }) {
2022-01-27 00:11:14 +01:00
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);
}
2023-06-22 01:07:19 +02:00
async user ({ userid }) {
2022-01-27 00:11:14 +01:00
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;
}
2023-06-22 01:07:19 +02:00
async settingsIndex () {
2022-01-27 00:11:14 +01:00
2023-06-22 01:07:19 +02:00
if (this.cache['settings-index']?.last < Date.now() - 3600_000)
return this.cache['settings-index'].value;
2022-01-27 00:11:14 +01:00
const evalFunc = (client) => {
const result = client.registry.settings.reduce((acc, setting) => {
2023-06-22 01:07:19 +02:00
if (!acc[setting.module.name])
acc[setting.module.name] = [];
2022-01-27 00:11:14 +01:00
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;
}
2023-06-22 01:07:19 +02:00
async getLiveGuild (message) { // eslint-disable-line no-unused-vars
2022-01-27 00:11:14 +01:00
const { guildId } = message;
const evalFunc = (client, { guildId }) => {
2022-07-28 09:46:43 +02:00
try {
const wrapper = client.getGuildWrapper(guildId);
return wrapper.toJSON();
} catch {
return null;
}
2022-01-27 00:11:14 +01:00
};
2023-06-22 01:07:19 +02:00
this.#client.logger.debug(`guild-live request - shard: ${message.shard}, message id ${message.id}`);
2022-01-27 00:11:14 +01:00
const result = await this.client.shardingManager.broadcastEval(evalFunc, { context: { guildId } });
2022-07-28 09:46:43 +02:00
const guild = result.find((elem) => elem !== null);
2022-01-27 00:11:14 +01:00
return guild;
}
2023-06-22 01:07:19 +02:00
async restartAPIShards (message) {
2022-07-24 10:46:51 +02:00
const { shards } = message;
await this.apiManager.restartShards(shards);
}
2022-01-27 00:11:14 +01:00
}
2023-06-22 01:07:19 +02:00
export default ApiClientUtil;