forked from Galactic/galactic-bot
Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
5ca8dfbfc9 |
@ -1,26 +0,0 @@
|
|||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- '*'
|
|
||||||
- '!master'
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
tests:
|
|
||||||
name: Tests
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
- name: Install node
|
|
||||||
uses: actions/setup-node@v4
|
|
||||||
with:
|
|
||||||
node-version: latest
|
|
||||||
registry-url: https://registry.corgi.wtf
|
|
||||||
cache: yarn
|
|
||||||
- name: Install deps
|
|
||||||
run: yarn install
|
|
||||||
- name: Lint project
|
|
||||||
run: yarn run eslint src/
|
|
||||||
- name: Build project
|
|
||||||
run: yarn build
|
|
||||||
env:
|
|
||||||
CI: false
|
|
@ -210,6 +210,14 @@ class DiscordClient extends Client
|
|||||||
this.emit('invalidRequestWarning', ...args);
|
this.emit('invalidRequestWarning', ...args);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// this.once('ready', () => {
|
||||||
|
// this._setActivity();
|
||||||
|
|
||||||
|
// setInterval(() => {
|
||||||
|
// this._setActivity();
|
||||||
|
// }, 1800000); // I think this is 30 minutes. I could be wrong.
|
||||||
|
// });
|
||||||
|
|
||||||
this.#loadEevents();
|
this.#loadEevents();
|
||||||
|
|
||||||
// process.on('uncaughtException', (err) => {
|
// process.on('uncaughtException', (err) => {
|
||||||
@ -222,13 +230,7 @@ class DiscordClient extends Client
|
|||||||
});
|
});
|
||||||
|
|
||||||
process.on('message', this.#handleMessage.bind(this));
|
process.on('message', this.#handleMessage.bind(this));
|
||||||
process.on('SIGINT', () => this.logger.info('Received SIGINT'));
|
process.on('SIGINT', () => this.shutdown());
|
||||||
process.on('SIGTERM', () => this.logger.info('Received SIGTERM'));
|
|
||||||
process.on('disconnect', () =>
|
|
||||||
{
|
|
||||||
this.logger.info('Process disconnected from parent for some reason, exiting');
|
|
||||||
this.shutdown(1);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async build ()
|
async build ()
|
||||||
@ -269,7 +271,7 @@ class DiscordClient extends Client
|
|||||||
this.#activityInterval = setInterval(() =>
|
this.#activityInterval = setInterval(() =>
|
||||||
{
|
{
|
||||||
this.setActivity();
|
this.setActivity();
|
||||||
}, Util.random(5, 10) * 60 * 1000);
|
}, Util.random(5, 10) * 60 * 60 * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.#built = true;
|
this.#built = true;
|
||||||
@ -294,8 +296,6 @@ class DiscordClient extends Client
|
|||||||
// Handle misc. messages.
|
// Handle misc. messages.
|
||||||
if (message._mEvalResult)
|
if (message._mEvalResult)
|
||||||
this.evalResult(message);
|
this.evalResult(message);
|
||||||
if (message._shutdown)
|
|
||||||
this.shutdown();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||||
@ -397,10 +397,6 @@ class DiscordClient extends Client
|
|||||||
this.user?.setActivity(`${userCount} users`, { type: ActivityType.Listening });
|
this.user?.setActivity(`${userCount} users`, { type: ActivityType.Listening });
|
||||||
},
|
},
|
||||||
async () =>
|
async () =>
|
||||||
{
|
|
||||||
this.user?.setActivity('out for troublemakers', { type: ActivityType.Watching });
|
|
||||||
},
|
|
||||||
async () =>
|
|
||||||
{
|
{
|
||||||
this.user?.setActivity('for /help', { type: ActivityType.Listening });
|
this.user?.setActivity('for /help', { type: ActivityType.Listening });
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
import { userInfo } from 'os';
|
import { userInfo } from 'os';
|
||||||
import { Util as U, FilterUtil as FU } from '../../../../utilities/index.js';
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
|
// @ts-ignore
|
||||||
|
import { Util, FilterUtil } from '../../../../utilities/index.js';
|
||||||
import { inspect } from 'util';
|
import { inspect } from 'util';
|
||||||
import { CommandOptionType, CommandParams } from '../../../../../@types/Client.js';
|
import { CommandOptionType, CommandParams } from '../../../../../@types/Client.js';
|
||||||
import DiscordClient from '../../../DiscordClient.js';
|
import DiscordClient from '../../../DiscordClient.js';
|
||||||
@ -10,8 +12,6 @@ import { ReplyOptions } from '../../../../../@types/Wrappers.js';
|
|||||||
const { username } = userInfo();
|
const { username } = userInfo();
|
||||||
class EvalCommand extends Command
|
class EvalCommand extends Command
|
||||||
{
|
{
|
||||||
static Util = U;
|
|
||||||
static FilterUtil = FU;
|
|
||||||
constructor (client: DiscordClient)
|
constructor (client: DiscordClient)
|
||||||
{
|
{
|
||||||
super(client, {
|
super(client, {
|
||||||
@ -32,10 +32,6 @@ class EvalCommand extends Command
|
|||||||
if (async?.asBool)
|
if (async?.asBool)
|
||||||
params = `(async () => {${params}})()`;
|
params = `(async () => {${params}})()`;
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
||||||
// @ts-ignore // declared for ease of use
|
|
||||||
const { Util, FilterUtil } = EvalCommand;
|
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
// @ts-ignore // declared for ease of use
|
// @ts-ignore // declared for ease of use
|
||||||
const { guild, author, member, client } = invoker;
|
const { guild, author, member, client } = invoker;
|
||||||
|
@ -56,76 +56,91 @@ class PollCommand extends SlashCommand
|
|||||||
|
|
||||||
async execute (invoker: InvokerWrapper<true>, { choices, channel, duration, multichoice, message }: CommandParams)
|
async execute (invoker: InvokerWrapper<true>, { choices, channel, duration, multichoice, message }: CommandParams)
|
||||||
{
|
{
|
||||||
const { subcommand } = invoker;
|
const { subcommand, author } = invoker;
|
||||||
|
const guild = invoker.guild!;
|
||||||
|
const member = invoker.member!;
|
||||||
|
|
||||||
if (subcommand!.name === 'create')
|
if (subcommand!.name === 'create')
|
||||||
return this.#createPoll(invoker, { choices, channel, duration, multichoice });
|
{
|
||||||
|
const targetChannel = (channel?.asChannel || invoker.channel) as GuildTextBasedChannel;
|
||||||
|
if (!targetChannel?.isTextBased())
|
||||||
|
throw new CommandError(invoker, { index: 'ERR_INVALID_CHANNEL_TYPE' });
|
||||||
|
const botMissing = targetChannel.permissionsFor(this.client.user!)?.missing([ 'SendMessages', 'EmbedLinks' ]);
|
||||||
|
const userMissing = targetChannel.permissionsFor(member).missing([ 'SendMessages' ]);
|
||||||
|
if (!botMissing || botMissing.length)
|
||||||
|
return invoker.editReply({ index: 'COMMAND_POLL_BOT_PERMS', params: { missing: botMissing?.join(', ') ?? 'ALL', channel: targetChannel.id } });
|
||||||
|
if (userMissing.length)
|
||||||
|
return invoker.editReply({ index: 'COMMAND_POLL_USER_PERMS', params: { missing: userMissing.join(', '), channel: targetChannel.id } });
|
||||||
|
|
||||||
|
const questions = await this.#queryQuestions(invoker, choices?.asNumber ?? 1, targetChannel);
|
||||||
|
if (!questions)
|
||||||
|
return;
|
||||||
|
|
||||||
|
await invoker.editReply({ index: 'COMMAND_POLL_STARTING' });
|
||||||
|
|
||||||
|
const embed = {
|
||||||
|
title: guild.format('COMMAND_POLL_TITLE', { user: author.tag }),
|
||||||
|
description: guild.format('COMMAND_POLL_DESCRIPTION', {
|
||||||
|
multi: guild.format('COMMAND_POLL_MULTI', { multichoice: multichoice?.asBool && questions.length > 1 || false }, { code: true }),
|
||||||
|
duration: duration ? guild.format('COMMAND_POLL_DURATION', { time: Util.humanise(duration.asNumber), duration: Math.floor(Date.now() / 1000) + duration.asNumber }) : guild.format('COMMAND_POLL_INFINITE')
|
||||||
|
}),
|
||||||
|
fields: questions,
|
||||||
|
color: EmbedDefaultColor,
|
||||||
|
timestamp: new Date().toISOString()
|
||||||
|
};
|
||||||
|
|
||||||
|
let pollMsg = null;
|
||||||
|
if (questions.length === 1)
|
||||||
|
{
|
||||||
|
questions[0].name = guild.format('COMMAND_POLL_FIELD_QUESTION');
|
||||||
|
pollMsg = await targetChannel.send({ embeds: [ embed ] });
|
||||||
|
await pollMsg.react('👍');
|
||||||
|
await pollMsg.react('👎');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
pollMsg = await targetChannel.send({ embeds: [ embed ] });
|
||||||
|
for (const question of questions)
|
||||||
|
await pollMsg.react(PollReactions.Multi[question.index + 1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
const poll: PollData = {
|
||||||
|
questions: questions.map(q => q.value),
|
||||||
|
duration: (duration?.asNumber ?? 0),
|
||||||
|
multiChoice: multichoice?.asBool && questions.length > 1 || false,
|
||||||
|
user: author.id,
|
||||||
|
channel: targetChannel.id,
|
||||||
|
startedIn: invoker.channel!.id,
|
||||||
|
message: pollMsg.id
|
||||||
|
};
|
||||||
|
|
||||||
|
if (duration)
|
||||||
|
await this.client.polls.create(poll, guild.id);
|
||||||
|
await invoker.editReply({ emoji: 'success', index: 'COMMAND_POLL_START', params: { channel: targetChannel.id } });
|
||||||
|
}
|
||||||
else if (subcommand!.name === 'delete')
|
else if (subcommand!.name === 'delete')
|
||||||
return this.#deletePoll(invoker, { message });
|
{
|
||||||
|
const poll = await this.client.polls.delete(message!.asString);
|
||||||
|
if (!poll)
|
||||||
|
return { index: 'COMMAND_POLL_404', emoji: 'failure' };
|
||||||
|
const pollChannel = await guild.resolveChannel<TextChannel>(poll.payload.channel);
|
||||||
|
if (!pollChannel)
|
||||||
|
return { index: 'COMMAND_POLL_MISSING_CHANNEL', emoji: 'failure' };
|
||||||
|
const msg = await pollChannel.messages.fetch(poll.payload.message).catch(() => null);
|
||||||
|
if (msg)
|
||||||
|
await msg.delete();
|
||||||
|
return { index: 'COMMAND_POLL_DELETED', emoji: 'success' };
|
||||||
|
}
|
||||||
else if (subcommand!.name === 'end')
|
else if (subcommand!.name === 'end')
|
||||||
return this.#endPoll(invoker, { message });
|
|
||||||
}
|
|
||||||
|
|
||||||
async #createPoll (invoker: InvokerWrapper<true>, { choices, channel, duration, multichoice }: CommandParams)
|
|
||||||
{
|
|
||||||
const { guild, member, author } = invoker;
|
|
||||||
const targetChannel = (channel?.asChannel || invoker.channel) as GuildTextBasedChannel;
|
|
||||||
if (!targetChannel?.isTextBased())
|
|
||||||
throw new CommandError(invoker, { index: 'ERR_INVALID_CHANNEL_TYPE' });
|
|
||||||
const botMissing = targetChannel.permissionsFor(this.client.user!)?.missing([ 'SendMessages', 'EmbedLinks' ]);
|
|
||||||
const userMissing = targetChannel.permissionsFor(member).missing([ 'SendMessages' ]);
|
|
||||||
if (!botMissing || botMissing.length)
|
|
||||||
return invoker.editReply({ index: 'COMMAND_POLL_BOT_PERMS', params: { missing: botMissing?.join(', ') ?? 'ALL', channel: targetChannel.id } });
|
|
||||||
if (userMissing.length)
|
|
||||||
return invoker.editReply({ index: 'COMMAND_POLL_USER_PERMS', params: { missing: userMissing.join(', '), channel: targetChannel.id } });
|
|
||||||
|
|
||||||
const questions = await this.#queryQuestions(invoker, choices?.asNumber ?? 1, targetChannel);
|
|
||||||
if (!questions)
|
|
||||||
return;
|
|
||||||
|
|
||||||
await invoker.editReply({ index: 'COMMAND_POLL_STARTING' });
|
|
||||||
|
|
||||||
const embed = {
|
|
||||||
title: guild.format('COMMAND_POLL_TITLE', { user: author.tag }),
|
|
||||||
description: guild.format('COMMAND_POLL_DESCRIPTION', {
|
|
||||||
multi: guild.format('COMMAND_POLL_MULTI', { multichoice: multichoice?.asBool && questions.length > 1 || false }, { code: true }),
|
|
||||||
duration: duration ? guild.format('COMMAND_POLL_DURATION', { time: Util.humanise(duration.asNumber), duration: Math.floor(Date.now() / 1000) + duration.asNumber }) : guild.format('COMMAND_POLL_INFINITE')
|
|
||||||
}),
|
|
||||||
fields: questions,
|
|
||||||
color: EmbedDefaultColor,
|
|
||||||
timestamp: new Date().toISOString()
|
|
||||||
};
|
|
||||||
|
|
||||||
let pollMsg = null;
|
|
||||||
if (questions.length === 1)
|
|
||||||
{
|
{
|
||||||
questions[0].name = guild.format('COMMAND_POLL_FIELD_QUESTION');
|
const poll = await this.client.polls.find(message!.asString);
|
||||||
pollMsg = await targetChannel.send({ embeds: [ embed ] });
|
if (!poll)
|
||||||
await pollMsg.react('👍');
|
return { index: 'COMMAND_POLL_404', emoji: 'failure' };
|
||||||
await pollMsg.react('👎');
|
await this.client.polls.end(poll.payload);
|
||||||
|
// await guild._poll(poll.data as PollData & CallbackData);
|
||||||
|
return { index: 'COMMAND_POLL_ENDED', emoji: 'success' };
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
pollMsg = await targetChannel.send({ embeds: [ embed ] });
|
|
||||||
for (const question of questions)
|
|
||||||
await pollMsg.react(PollReactions.Multi[question.index + 1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
const poll: PollData = {
|
|
||||||
questions: questions.map(q => q.value),
|
|
||||||
duration: (duration?.asNumber ?? 0),
|
|
||||||
multiChoice: multichoice?.asBool && questions.length > 1 || false,
|
|
||||||
user: author.id,
|
|
||||||
channel: targetChannel.id,
|
|
||||||
startedIn: invoker.channel!.id,
|
|
||||||
message: pollMsg.id
|
|
||||||
};
|
|
||||||
|
|
||||||
if (duration)
|
|
||||||
await this.client.polls.create(poll, guild.id);
|
|
||||||
await invoker.editReply({ emoji: 'success', index: 'COMMAND_POLL_START', params: { channel: targetChannel.id } });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async #queryQuestions (invoker: InvokerWrapper<true>, choices: number, targetchannel: GuildTextBasedChannel)
|
async #queryQuestions (invoker: InvokerWrapper<true>, choices: number, targetchannel: GuildTextBasedChannel)
|
||||||
{
|
{
|
||||||
const { guild, member } = invoker;
|
const { guild, member } = invoker;
|
||||||
@ -151,31 +166,6 @@ class PollCommand extends SlashCommand
|
|||||||
}
|
}
|
||||||
return questions;
|
return questions;
|
||||||
}
|
}
|
||||||
|
|
||||||
async #deletePoll (invoker: InvokerWrapper<true>, { message }: CommandParams)
|
|
||||||
{
|
|
||||||
const { guild } = invoker;
|
|
||||||
const poll = await this.client.polls.delete(message!.asString);
|
|
||||||
if (!poll)
|
|
||||||
return { index: 'COMMAND_POLL_404', emoji: 'failure' };
|
|
||||||
const pollChannel = await guild.resolveChannel<TextChannel>(poll.payload.channel);
|
|
||||||
if (!pollChannel)
|
|
||||||
return { index: 'COMMAND_POLL_MISSING_CHANNEL', emoji: 'failure' };
|
|
||||||
const msg = await pollChannel.messages.fetch(poll.payload.message).catch(() => null);
|
|
||||||
if (msg)
|
|
||||||
await msg.delete();
|
|
||||||
return { index: 'COMMAND_POLL_DELETED', emoji: 'success' };
|
|
||||||
}
|
|
||||||
|
|
||||||
async #endPoll (_invoker: InvokerWrapper<true>, { message }: CommandParams)
|
|
||||||
{
|
|
||||||
const poll = await this.client.polls.find(message!.asString);
|
|
||||||
if (!poll)
|
|
||||||
return { index: 'COMMAND_POLL_404', emoji: 'failure' };
|
|
||||||
await this.client.polls.end(poll._id, poll.payload);
|
|
||||||
// await guild._poll(poll.data as PollData & CallbackData);
|
|
||||||
return { index: 'COMMAND_POLL_ENDED', emoji: 'success' };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default PollCommand;
|
export default PollCommand;
|
@ -185,9 +185,9 @@ class CallbackManager implements Initialisable
|
|||||||
*
|
*
|
||||||
* @async
|
* @async
|
||||||
* @param {string} id
|
* @param {string} id
|
||||||
* @returns {Promise<void>}
|
* @returns {*}
|
||||||
*/
|
*/
|
||||||
async removeCallback (id: string): Promise<void>
|
async removeCallback (id: string)
|
||||||
{
|
{
|
||||||
await this.storage.deleteOne({ _id: id });
|
await this.storage.deleteOne({ _id: id });
|
||||||
const timeout = this.#timeouts.get(id);
|
const timeout = this.#timeouts.get(id);
|
||||||
|
@ -591,9 +591,9 @@ class ModerationManager implements Initialisable, CallbackClient
|
|||||||
return responses;
|
return responses;
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleCallback (_id: string, infraction: InfractionJSON)
|
async handleCallback (id: string)
|
||||||
{
|
{
|
||||||
// const infraction = await this.#client.mongodb.infractions.findOne({ id });
|
const infraction = await this.#client.mongodb.infractions.findOne({ id });
|
||||||
if (!infraction)
|
if (!infraction)
|
||||||
return;
|
return;
|
||||||
this.#logger.debug(`Infraction callback: ${infraction.id} (${infraction.type})`);
|
this.#logger.debug(`Infraction callback: ${infraction.id} (${infraction.type})`);
|
||||||
@ -667,7 +667,7 @@ class ModerationManager implements Initialisable, CallbackClient
|
|||||||
if (expiresAt - currentDate <= 0)
|
if (expiresAt - currentDate <= 0)
|
||||||
{
|
{
|
||||||
this.#logger.debug(`Expired infraction:\n${inspect(infraction)}`);
|
this.#logger.debug(`Expired infraction:\n${inspect(infraction)}`);
|
||||||
return this.handleCallback(infraction.id, infraction);
|
return this.handleCallback(infraction.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.#logger.debug(`Creating infraction callback for ${infraction.id} (${infraction.type}), expiring in ${Util.humanise(duration / 1000)}`);
|
this.#logger.debug(`Creating infraction callback for ${infraction.id} (${infraction.type}), expiring in ${Util.humanise(duration / 1000)}`);
|
||||||
|
@ -47,12 +47,12 @@ class PollManager implements CallbackClient
|
|||||||
return poll;
|
return poll;
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleCallback (id: string, payload: PollData): Promise<void>
|
async handleCallback (_id: string, payload: PollData): Promise<void>
|
||||||
{
|
{
|
||||||
await this.end(id, payload);
|
await this.end(payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
async end (id: string, { user, channel, startedIn, message, multiChoice }: PollData)
|
async end ({ user, channel, startedIn, message, multiChoice }: PollData)
|
||||||
{
|
{
|
||||||
const startChannel = await this.#client.resolveChannel(startedIn);
|
const startChannel = await this.#client.resolveChannel(startedIn);
|
||||||
const pollChannel = await this.#client.resolveChannel(channel);
|
const pollChannel = await this.#client.resolveChannel(channel);
|
||||||
@ -89,7 +89,6 @@ class PollManager implements CallbackClient
|
|||||||
if (startChannel && startChannel.isTextBased())
|
if (startChannel && startChannel.isTextBased())
|
||||||
await startChannel.send(guild.format('COMMAND_POLL_NOTIFY_STARTER', { user, channel }));
|
await startChannel.send(guild.format('COMMAND_POLL_NOTIFY_STARTER', { user, channel }));
|
||||||
}
|
}
|
||||||
await this.callbacks.removeCallback(id);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,47 +1,27 @@
|
|||||||
import {
|
import { EventEmitter } from 'node:events';
|
||||||
EventEmitter
|
import { inspect } from 'node:util';
|
||||||
} from 'node:events';
|
|
||||||
|
|
||||||
import {
|
|
||||||
inspect
|
|
||||||
} from 'node:util';
|
|
||||||
|
|
||||||
import {
|
|
||||||
existsSync
|
|
||||||
} from 'node:fs';
|
|
||||||
|
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
|
|
||||||
import {
|
import { CommandsDef, IPCMessage } from '../../@types/Shared.js';
|
||||||
CommandsDef,
|
import { BroadcastEvalOptions, ShardMethod, ShardingOptions } from '../../@types/Shard.js';
|
||||||
IPCMessage
|
import { ControllerOptions } from '../../@types/Controller.js';
|
||||||
} from '../../@types/Shared.js';
|
|
||||||
|
|
||||||
import {
|
import { MasterLogger } from '@navy.gif/logger';
|
||||||
BroadcastEvalOptions,
|
import { Collection } from 'discord.js';
|
||||||
ShardMethod,
|
|
||||||
ShardingOptions
|
|
||||||
} from '../../@types/Shard.js';
|
|
||||||
|
|
||||||
import {
|
|
||||||
ControllerOptions
|
|
||||||
} from '../../@types/Controller.js';
|
|
||||||
|
|
||||||
import {
|
|
||||||
MasterLogger
|
|
||||||
} from '@navy.gif/logger';
|
|
||||||
|
|
||||||
import {
|
|
||||||
Collection
|
|
||||||
} from 'discord.js';
|
|
||||||
|
|
||||||
// Available for evals
|
// Available for evals
|
||||||
import ClientUtils from './ClientUtils.js';
|
import ClientUtils from './ClientUtils.js';
|
||||||
import Metrics from './Metrics.js';
|
import Metrics from './Metrics.js';
|
||||||
// import ApiClientUtil from './ApiClientUtil.js';
|
// import ApiClientUtil from './ApiClientUtil.js';
|
||||||
import SlashCommandManager from './SlashCommandManager.js';
|
import SlashCommandManager from './rest/SlashCommandManager.js';
|
||||||
|
import { Shard } from './shard/index.js';
|
||||||
|
import { existsSync } from 'node:fs';
|
||||||
import Util from '../utilities/Util.js';
|
import Util from '../utilities/Util.js';
|
||||||
import Shard from './Shard.js';
|
|
||||||
|
// Placeholder
|
||||||
|
type GalacticAPI = {
|
||||||
|
init: () => Promise<void>
|
||||||
|
}
|
||||||
|
|
||||||
class Controller extends EventEmitter
|
class Controller extends EventEmitter
|
||||||
{
|
{
|
||||||
@ -59,6 +39,7 @@ class Controller extends EventEmitter
|
|||||||
#readyAt: number | null;
|
#readyAt: number | null;
|
||||||
#built: boolean;
|
#built: boolean;
|
||||||
|
|
||||||
|
#api?: GalacticAPI;
|
||||||
clientUtils: typeof ClientUtils;
|
clientUtils: typeof ClientUtils;
|
||||||
|
|
||||||
constructor (options: ControllerOptions, version: string)
|
constructor (options: ControllerOptions, version: string)
|
||||||
@ -111,7 +92,28 @@ class Controller extends EventEmitter
|
|||||||
async build ()
|
async build ()
|
||||||
{
|
{
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
|
// const API = this._options.api.load ? await import('/Documents/My programs/GBot/api/index.js')
|
||||||
|
// .catch(() => this.logger.warn(`Error importing API files, continuing without`)) : null;
|
||||||
|
|
||||||
|
// let API = null;
|
||||||
|
// if (this.#options.api.load)
|
||||||
|
// API = await import('../../api/index.js').catch(() => this.#logger.warn(`Error importing API files, continuing without`));
|
||||||
|
// if (API) {
|
||||||
|
// this.#logger.info('Booting up API');
|
||||||
|
// const { default: APIManager } = API;
|
||||||
|
// this.#api = new APIManager(this, this.#options.api) as GalacticAPI;
|
||||||
|
// await this.#api.init();
|
||||||
|
// const now = Date.now();
|
||||||
|
// this.#logger.info(`API ready. Took ${now - start} ms`);
|
||||||
|
// start = now;
|
||||||
|
// }
|
||||||
|
|
||||||
this.#logger.status('Starting bot shards');
|
this.#logger.status('Starting bot shards');
|
||||||
|
// await this.shardingManager.spawn().catch((error) => {
|
||||||
|
// this.#logger.error(`Fatal error during shard spawning:\n${error.stack || inspect(error)}`);
|
||||||
|
// // eslint-disable-next-line no-process-exit
|
||||||
|
// process.exit(); // Prevent a boot loop when shards die due to an error in the client
|
||||||
|
// });
|
||||||
|
|
||||||
const { totalShards, token } = this.#shardingOptions;
|
const { totalShards, token } = this.#shardingOptions;
|
||||||
let shardCount = 0;
|
let shardCount = 0;
|
||||||
@ -131,6 +133,7 @@ class Controller extends EventEmitter
|
|||||||
throw new TypeError('Amount of shards must be an integer.');
|
throw new TypeError('Amount of shards must be an integer.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// const promises = [];
|
||||||
const retry: Shard[] = [];
|
const retry: Shard[] = [];
|
||||||
for (let i = 0; i < shardCount; i++)
|
for (let i = 0; i < shardCount; i++)
|
||||||
{
|
{
|
||||||
@ -145,9 +148,9 @@ class Controller extends EventEmitter
|
|||||||
this.logger.info('Retrying shard', i);
|
this.logger.info('Retrying shard', i);
|
||||||
retry.push(shard);
|
retry.push(shard);
|
||||||
}
|
}
|
||||||
|
// promises.push();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retry failed spawns
|
|
||||||
for (const shard of retry)
|
for (const shard of retry)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@ -160,6 +163,8 @@ class Controller extends EventEmitter
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// await Promise.all(promises);
|
||||||
|
|
||||||
this.#logger.status(`Shards spawned, spawned ${this.#shards.size} shards. Took ${Date.now() - start} ms`);
|
this.#logger.status(`Shards spawned, spawned ${this.#shards.size} shards. Took ${Date.now() - start} ms`);
|
||||||
|
|
||||||
this.#built = true;
|
this.#built = true;
|
||||||
@ -168,11 +173,12 @@ class Controller extends EventEmitter
|
|||||||
|
|
||||||
async shutdown ()
|
async shutdown ()
|
||||||
{
|
{
|
||||||
this.logger.info('Received SIGINT or SIGTERM, shutting down');
|
this.logger.info('Received SIGINT, shutting down');
|
||||||
setTimeout(process.exit, 90_000);
|
setTimeout(process.exit, 90_000);
|
||||||
const promises = this.shards
|
const promises = this.shards
|
||||||
.filter(shard => shard.ready)
|
.filter(shard => shard.ready)
|
||||||
.map(shard => shard.kill());
|
.map(shard => shard.awaitShutdown()
|
||||||
|
.then(() => shard.removeAllListeners()));
|
||||||
if (promises.length)
|
if (promises.length)
|
||||||
await Promise.all(promises);
|
await Promise.all(promises);
|
||||||
this.logger.status('Shutdown complete, goodbye');
|
this.logger.status('Shutdown complete, goodbye');
|
||||||
@ -400,6 +406,11 @@ class Controller extends EventEmitter
|
|||||||
return this.#logger;
|
return this.#logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get api ()
|
||||||
|
{
|
||||||
|
return this.#api;
|
||||||
|
}
|
||||||
|
|
||||||
get shards ()
|
get shards ()
|
||||||
{
|
{
|
||||||
return this.#shards.clone();
|
return this.#shards.clone();
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import BaseClient from './Controller.js';
|
import BaseClient from './Controller.js';
|
||||||
import os from 'node:os';
|
import os from 'node:os';
|
||||||
import Shard from './Shard.js';
|
import Shard from './shard/Shard.js';
|
||||||
import { IPCMessage } from '../../@types/Shared.js';
|
import { IPCMessage } from '../../@types/Shared.js';
|
||||||
import djs, { GuildMessageManager } from 'discord.js';
|
import djs, { GuildMessageManager } from 'discord.js';
|
||||||
import DiscordClient from '../client/DiscordClient.js';
|
import DiscordClient from '../client/DiscordClient.js';
|
||||||
|
@ -5,8 +5,8 @@ import fs from 'node:fs';
|
|||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import { inspect } from 'node:util';
|
import { inspect } from 'node:util';
|
||||||
|
|
||||||
import BaseClient from './Controller.js';
|
import BaseClient from '../Controller.js';
|
||||||
import { Command, CommandOption, CommandsDef } from '../../@types/Shared.js';
|
import { Command, CommandOption, CommandsDef } from '../../../@types/Shared.js';
|
||||||
|
|
||||||
type CommandHashTable = {
|
type CommandHashTable = {
|
||||||
global: string | null,
|
global: string | null,
|
@ -1,14 +1,13 @@
|
|||||||
import EventEmitter from 'node:events';
|
import EventEmitter from 'node:events';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import childProcess, { ChildProcess } from 'node:child_process';
|
|
||||||
import { makePlainError, makeError, MakeErrorOptions } from 'discord.js';
|
import { makePlainError, makeError, MakeErrorOptions } from 'discord.js';
|
||||||
|
import { Util } from '../../utilities/index.js';
|
||||||
|
import childProcess, { ChildProcess } from 'node:child_process';
|
||||||
|
|
||||||
import Controller from './Controller.js';
|
import { EnvObject, IPCMessage } from '../../../@types/Shared.js';
|
||||||
import { Util } from '../utilities/index.js';
|
import Controller from '../Controller.js';
|
||||||
|
import { ShardOptions } from '../../../@types/Shard.js';
|
||||||
import { ShardOptions } from '../../@types/Shard.js';
|
import { ClientOptions } from '../../../@types/Client.js';
|
||||||
import { ClientOptions } from '../../@types/Client.js';
|
|
||||||
import { EnvObject, IPCMessage } from '../../@types/Shared.js';
|
|
||||||
|
|
||||||
const KillTO = 90 * 1000;
|
const KillTO = 90 * 1000;
|
||||||
|
|
||||||
@ -17,8 +16,8 @@ class Shard extends EventEmitter
|
|||||||
[key: string]: unknown;
|
[key: string]: unknown;
|
||||||
|
|
||||||
#id: number;
|
#id: number;
|
||||||
#controller: Controller;
|
#manager: Controller;
|
||||||
#env: EnvObject;
|
#env: EnvObject; // { [key: string]: string | boolean | number };
|
||||||
#ready: boolean;
|
#ready: boolean;
|
||||||
#clientOptions: ClientOptions;
|
#clientOptions: ClientOptions;
|
||||||
|
|
||||||
@ -39,10 +38,10 @@ class Shard extends EventEmitter
|
|||||||
#crashes: number[];
|
#crashes: number[];
|
||||||
#fatal: boolean;
|
#fatal: boolean;
|
||||||
|
|
||||||
constructor (controller: Controller, id: number, options: ShardOptions)
|
constructor (manager: Controller, id: number, options: ShardOptions)
|
||||||
{
|
{
|
||||||
super();
|
super();
|
||||||
this.#controller = controller;
|
this.#manager = manager;
|
||||||
this.#id = id;
|
this.#id = id;
|
||||||
|
|
||||||
this.#args = options.args ?? [];
|
this.#args = options.args ?? [];
|
||||||
@ -371,7 +370,7 @@ class Shard extends EventEmitter
|
|||||||
if (message._sFetchProp)
|
if (message._sFetchProp)
|
||||||
{
|
{
|
||||||
const resp = { _sFetchProp: message._sFetchProp, _sFetchPropShard: message._sFetchPropShard };
|
const resp = { _sFetchProp: message._sFetchProp, _sFetchPropShard: message._sFetchPropShard };
|
||||||
this.#controller.fetchClientValues(message._sFetchProp, message._sFetchPropShard).then(
|
this.#manager.fetchClientValues(message._sFetchProp, message._sFetchPropShard).then(
|
||||||
(results: unknown) => this.send({ ...resp, _result: results }),
|
(results: unknown) => this.send({ ...resp, _result: results }),
|
||||||
(error: Error) => this.send({ ...resp, _error: makePlainError(error) })
|
(error: Error) => this.send({ ...resp, _error: makePlainError(error) })
|
||||||
);
|
);
|
||||||
@ -381,7 +380,7 @@ class Shard extends EventEmitter
|
|||||||
if (message._sEval)
|
if (message._sEval)
|
||||||
{
|
{
|
||||||
const resp = { _sEval: message._sEval, _sEvalShard: message._sEvalShard };
|
const resp = { _sEval: message._sEval, _sEvalShard: message._sEvalShard };
|
||||||
this.#controller._performOnShards('eval', [ message._sEval ], message._sEvalShard).then(
|
this.#manager._performOnShards('eval', [ message._sEval ], message._sEvalShard).then(
|
||||||
(results: unknown) => this.send({ ...resp, _result: results }),
|
(results: unknown) => this.send({ ...resp, _result: results }),
|
||||||
(error: Error) => this.send({ ...resp, _error: makePlainError(error) })
|
(error: Error) => this.send({ ...resp, _error: makePlainError(error) })
|
||||||
);
|
);
|
||||||
@ -391,7 +390,7 @@ class Shard extends EventEmitter
|
|||||||
if (message._sRespawnAll)
|
if (message._sRespawnAll)
|
||||||
{
|
{
|
||||||
const { shardDelay, respawnDelay, timeout } = message._sRespawnAll;
|
const { shardDelay, respawnDelay, timeout } = message._sRespawnAll;
|
||||||
this.#controller.respawnAll({ shardDelay, respawnDelay, timeout }).catch(() =>
|
this.#manager.respawnAll({ shardDelay, respawnDelay, timeout }).catch(() =>
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
});
|
});
|
6
src/middleware/shard/index.ts
Normal file
6
src/middleware/shard/index.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import Shard from './Shard.js';
|
||||||
|
// import ShardingManager from './ShardingManager.js';
|
||||||
|
export {
|
||||||
|
Shard,
|
||||||
|
// ShardingManager
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user