Refactor Amayo client initialization and enhance editor functionality

- Updated the Amayo client to use environment variables for configuration defaults.
- Improved cache and sweeper settings for message and user management.
- Added detailed error handling and logging during database connection and Discord login.
- Introduced a new interactive editor for creating and managing display components with modals.
- Implemented various editor actions including adding content, editing titles/descriptions, and managing components.
- Enhanced user feedback with ephemeral messages for actions taken in the editor.
This commit is contained in:
Shni
2025-10-14 11:12:52 -05:00
parent 6482fbc8ea
commit 0eb5aa0691
3 changed files with 2403 additions and 974 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,27 @@
import { Client, GatewayIntentBits, Options, Partials } from 'discord.js';
import { prisma, ensurePrismaConnection } from './database/prisma';
import logger from './lib/logger';
import {
Client,
GatewayIntentBits,
Options,
Partials,
ClientOptions,
} from "discord.js";
import { prisma, ensurePrismaConnection } from "./database/prisma";
import logger from "./lib/logger";
const DEFAULTS = {
CACHE_MESSAGES_LIMIT: 50,
CACHE_MEMBERS_LIMIT: 100,
SWEEP_MESSAGES_INTERVAL_SECONDS: 300,
SWEEP_MESSAGES_LIFETIME_SECONDS: 900,
USERS_SWEEP_INTERVAL_SECONDS: 60 * 30,
REST_RETRIES: 5,
} as const;
function intEnv(name: keyof typeof DEFAULTS, fallback?: number): number {
const raw = process.env[name];
const val = raw ? parseInt(raw, 10) : NaN;
return Number.isFinite(val) ? val : fallback ?? DEFAULTS[name];
}
class Amayo extends Client {
public key: string;
@@ -8,57 +29,67 @@ class Amayo extends Client {
public mode: string;
constructor() {
super({
// Build options here so `this` can be referenced in the users.sweep filter
const options: ClientOptions = {
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
GatewayIntentBits.MessageContent,
],
partials: [Partials.Channel, Partials.Message],
makeCache: Options.cacheWithLimits({
MessageManager: parseInt(process.env.CACHE_MESSAGES_LIMIT || '50', 10),
GuildMemberManager: parseInt(process.env.CACHE_MEMBERS_LIMIT || '100', 10),
MessageManager: intEnv("CACHE_MESSAGES_LIMIT"),
GuildMemberManager: intEnv("CACHE_MEMBERS_LIMIT"),
ThreadManager: 10,
ReactionManager: 0,
GuildInviteManager: 0,
StageInstanceManager: 0,
PresenceManager: 0
PresenceManager: 0,
}),
sweepers: {
messages: {
interval: parseInt(process.env.SWEEP_MESSAGES_INTERVAL_SECONDS || '300', 10),
lifetime: parseInt(process.env.SWEEP_MESSAGES_LIFETIME_SECONDS || '900', 10)
interval: intEnv("SWEEP_MESSAGES_INTERVAL_SECONDS"),
lifetime: intEnv("SWEEP_MESSAGES_LIFETIME_SECONDS"),
},
users: {
interval: 60 * 30,
filter: () => (user) => user.bot && user.id !== this.user?.id
}
interval: DEFAULTS.USERS_SWEEP_INTERVAL_SECONDS,
filter: () => (user) => user.bot && user.id !== this.user?.id,
},
},
rest: {
retries: 5
}
});
retries: DEFAULTS.REST_RETRIES,
},
};
this.key = process.env.TOKEN ?? '';
this.mode = process.env.MODE ?? 'Normal';
super(options);
this.key = process.env.TOKEN ?? "";
this.mode = process.env.MODE ?? "Normal";
}
async play() {
/**
* Inicia la conexión a la base de datos y al gateway de Discord.
* Lanza si falta la clave o si falla la conexión/login.
*/
public async play(): Promise<void> {
if (!this.key) {
logger.error('No key provided');
throw new Error('Missing DISCORD TOKEN');
} else {
logger.error("No key provided");
throw new Error("Missing DISCORD TOKEN");
}
try {
await ensurePrismaConnection();
logger.info('Successfully connected to the database (singleton).');
logger.info("Successfully connected to the database (singleton).");
await this.login(this.key);
} catch (error) {
logger.error({ err: error }, 'Failed to connect to DB or login to Discord');
logger.error(
{ err: error },
"Failed to connect to DB or login to Discord"
);
throw error;
}
}
}
}
export default Amayo;