107 lines
3.5 KiB
JavaScript
107 lines
3.5 KiB
JavaScript
|
class ApiClientUtil {
|
||
|
|
||
|
constructor(client) {
|
||
|
this.client = client;
|
||
|
|
||
|
this.cache = {};
|
||
|
|
||
|
}
|
||
|
|
||
|
async handleMessage(shard, message) {
|
||
|
|
||
|
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;
|
||
|
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 guild = client.guilds.resolve(guildId);
|
||
|
if (!guild) return null;
|
||
|
|
||
|
const wrapper = new client.wrapperClasses.GuildWrapper(client, guild);
|
||
|
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;
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
module.exports = ApiClientUtil;
|