2023-12-03 21:12:01 +01:00
|
|
|
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 = {
|
|
|
|
|
|
|
|
}
|
2023-12-03 21:12:01 +01:00
|
|
|
class ApiClientUtil
|
|
|
|
{
|
2023-06-22 01:07:19 +02:00
|
|
|
#client: BaseClient;
|
|
|
|
#cache: {};
|
|
|
|
|
2023-12-03 21:12:01 +01:00
|
|
|
constructor (client: BaseClient)
|
|
|
|
{
|
2023-06-22 01:07:19 +02:00
|
|
|
this.#client = client;
|
|
|
|
this.#cache = {};
|
2022-01-27 00:11:14 +01:00
|
|
|
}
|
|
|
|
|
2023-12-03 21:12:01 +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)
|
2023-12-03 21:12:01 +01:00
|
|
|
{
|
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
|
2023-12-03 21:12:01 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2023-12-03 21:12:01 +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-12-03 21:12:01 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-12-03 21:12:01 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-12-03 21:12:01 +01:00
|
|
|
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;
|
2023-12-03 21:12:01 +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-12-05 19:31:53 +01:00
|
|
|
async getLiveGuild (message)
|
2023-12-03 21:12:01 +01:00
|
|
|
{ // eslint-disable-line no-unused-vars
|
2022-01-27 00:11:14 +01:00
|
|
|
const { guildId } = message;
|
2023-12-03 21:12:01 +01:00
|
|
|
const evalFunc = (client, { guildId }) =>
|
|
|
|
{
|
2023-12-05 19:31:53 +01:00
|
|
|
try
|
2023-12-03 21:12:01 +01:00
|
|
|
{
|
2022-07-28 09:46:43 +02:00
|
|
|
const wrapper = client.getGuildWrapper(guildId);
|
|
|
|
return wrapper.toJSON();
|
2023-12-03 21:12:01 +01:00
|
|
|
}
|
|
|
|
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)
|
2023-12-03 21:12:01 +01:00
|
|
|
{
|
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;
|