galactic-bot/archive/ApiClientUtil.ts

139 lines
4.1 KiB
TypeScript
Raw Permalink Normal View History

import { IPCMessage } from '../@types/Shared.js';
import BaseClient from './Controller.js';
2022-01-27 00:11:14 +01:00
2023-12-05 19:31:53 +01:00
type APIShard = {
}
class ApiClientUtil
{
2023-06-22 01:07:19 +02:00
#client: BaseClient;
#cache: {};
constructor (client: BaseClient)
{
2023-06-22 01:07:19 +02:00
this.#client = client;
this.#cache = {};
2022-01-27 00:11:14 +01:00
}
get apiManager ()
{
2023-06-22 01:07:19 +02:00
return this.#client.api;
2022-07-24 10:46:51 +02:00
}
2023-12-05 19:31:53 +01: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
2023-12-05 19:31:53 +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;
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 });
}
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 }) =>
{
2022-01-27 00:11:14 +01:00
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 ()
{
2023-06-22 01:07:19 +02:00
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) =>
{
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-12-05 19:31:53 +01: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 }) =>
{
2023-12-05 19:31:53 +01:00
try
{
2022-07-28 09:46:43 +02:00
const wrapper = client.getGuildWrapper(guildId);
return wrapper.toJSON();
}
catch
{
2022-07-28 09:46:43 +02:00
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-12-05 19:31:53 +01:00
async restartAPIShards (message: IPCMessage)
{
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;