forked from Galactic/galactic-bot
139 lines
4.1 KiB
TypeScript
139 lines
4.1 KiB
TypeScript
import { IPCMessage } from '../@types/Shared.js';
|
|
import BaseClient from './Controller.js';
|
|
|
|
|
|
type APIShard = {
|
|
|
|
}
|
|
class ApiClientUtil
|
|
{
|
|
#client: BaseClient;
|
|
#cache: {};
|
|
|
|
constructor (client: BaseClient)
|
|
{
|
|
this.#client = client;
|
|
this.#cache = {};
|
|
}
|
|
|
|
get apiManager ()
|
|
{
|
|
return this.#client.api;
|
|
}
|
|
|
|
async handleMessage (shard: APIShard, message: IPCMessage)
|
|
{
|
|
const { type, id } = message;
|
|
let response = null;
|
|
|
|
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' });
|
|
}
|
|
}
|
|
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 }) =>
|
|
{
|
|
const wrapper = client.getGuildWrapper(guildId);
|
|
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 }) =>
|
|
{
|
|
try
|
|
{
|
|
const wrapper = client.getGuildWrapper(guildId);
|
|
return wrapper.toJSON();
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
};
|
|
|
|
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 !== null);
|
|
return guild;
|
|
}
|
|
|
|
async restartAPIShards (message: IPCMessage)
|
|
{
|
|
const { shards } = message;
|
|
await this.apiManager.restartShards(shards);
|
|
}
|
|
|
|
}
|
|
|
|
export default ApiClientUtil; |