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:
1052
src/.backup/createDisplayComponent.ts
Normal file
1052
src/.backup/createDisplayComponent.ts
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,64 +1,95 @@
|
||||
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;
|
||||
public prisma = prisma;
|
||||
public mode: string;
|
||||
public key: string;
|
||||
public prisma = prisma;
|
||||
public mode: string;
|
||||
|
||||
constructor() {
|
||||
super({
|
||||
intents: [
|
||||
GatewayIntentBits.Guilds,
|
||||
GatewayIntentBits.GuildMembers,
|
||||
GatewayIntentBits.GuildMessages,
|
||||
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),
|
||||
ThreadManager: 10,
|
||||
ReactionManager: 0,
|
||||
GuildInviteManager: 0,
|
||||
StageInstanceManager: 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)
|
||||
},
|
||||
users: {
|
||||
interval: 60 * 30,
|
||||
filter: () => (user) => user.bot && user.id !== this.user?.id
|
||||
}
|
||||
},
|
||||
rest: {
|
||||
retries: 5
|
||||
}
|
||||
});
|
||||
constructor() {
|
||||
// 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,
|
||||
],
|
||||
partials: [Partials.Channel, Partials.Message],
|
||||
makeCache: Options.cacheWithLimits({
|
||||
MessageManager: intEnv("CACHE_MESSAGES_LIMIT"),
|
||||
GuildMemberManager: intEnv("CACHE_MEMBERS_LIMIT"),
|
||||
ThreadManager: 10,
|
||||
ReactionManager: 0,
|
||||
GuildInviteManager: 0,
|
||||
StageInstanceManager: 0,
|
||||
PresenceManager: 0,
|
||||
}),
|
||||
sweepers: {
|
||||
messages: {
|
||||
interval: intEnv("SWEEP_MESSAGES_INTERVAL_SECONDS"),
|
||||
lifetime: intEnv("SWEEP_MESSAGES_LIFETIME_SECONDS"),
|
||||
},
|
||||
users: {
|
||||
interval: DEFAULTS.USERS_SWEEP_INTERVAL_SECONDS,
|
||||
filter: () => (user) => user.bot && user.id !== this.user?.id,
|
||||
},
|
||||
},
|
||||
rest: {
|
||||
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";
|
||||
}
|
||||
|
||||
/**
|
||||
* 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");
|
||||
}
|
||||
|
||||
async play() {
|
||||
if (!this.key) {
|
||||
logger.error('No key provided');
|
||||
throw new Error('Missing DISCORD TOKEN');
|
||||
} else {
|
||||
try {
|
||||
await ensurePrismaConnection();
|
||||
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');
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
try {
|
||||
await ensurePrismaConnection();
|
||||
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"
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default Amayo;
|
||||
Reference in New Issue
Block a user