enerthya.dev-pudding
v1.0.0
Published
Database abstraction layer for Enerthya — wraps all Mongoose schemas into a single injectable PuddingClient
Maintainers
Readme
enerthya.dev-pudding
Camada de abstração de banco de dados para a Enerthya. Inspirado em
pudding:client(MrPowerGamerBR / LorittaBot).
Encapsula todos os schemas Mongoose em um único PuddingClient injetável.
Comandos e handlers chamam métodos do pudding em vez de importar schemas diretamente
— mantendo a lógica de negócio desacoplada da camada de banco.
Zero dependências externas. Os schemas Mongoose são injetados na construção.
Por que usar
Sem pudding, um comando faz:
const { getSettings } = require("@schemas/Guild");
const { getMember } = require("@schemas/Member");
const { addModLogToDb } = require("@schemas/ModLog");Com pudding, qualquer handler recebe um único objeto:
const settings = await pudding.getGuild(guild);
const member = await pudding.getMember(guildId, memberId);
await pudding.addModLog(admin, target, reason, "BAN");Vantagens: testável com mocks, sem imports espalhados, método incrementXP agrega a lógica de XP + level-up em uma chamada só.
Uso básico
const { PuddingClient } = require("enerthya.dev-pudding");
// bot.js — criar uma vez após conectar ao banco
const pudding = new PuddingClient(EnerthyaCore.schemas);
pudding.enableXpCooldown(); // ativa o cache de cooldown de XP
// Em qualquer handler ou comando:
const settings = await pudding.getGuild(guild);
const member = await pudding.getMember(guildId, memberId);
const { leveledUp, newLevel } = await pudding.incrementXP(guildId, memberId, xpAmount);
await pudding.addModLog(admin, target, reason, "BAN");API completa
Guild
| Método | Parâmetros | Retorno |
|--------|-----------|---------|
| getGuild(guild) | Discord Guild | Promise<doc> |
| clearGuildCache(guildId) | string | void |
User
| Método | Parâmetros | Retorno |
|--------|-----------|---------|
| getUser(user) | Discord User | Promise<doc> |
| getReputationLb(limit?) | number | Promise<doc[]> |
| getEconomyGlobalLb(limit?) | number | Promise<doc[]> |
| getEconomyLocalLb(memberIds, limit?) | string[], number | Promise<doc[]> |
Member (invites, strikes)
| Método | Parâmetros | Retorno |
|--------|-----------|---------|
| getMember(guildId, memberId) | string, string | Promise<doc> |
| getInvitesLb(guildId, limit?) | string, number | Promise<doc[]> |
MemberStats (XP, level, atividade)
| Método | Parâmetros | Retorno |
|--------|-----------|---------|
| getMemberStats(guildId, memberId) | string, string | Promise<doc> |
| getXpLb(guildId, limit?) | string, number | Promise<doc[]> |
| incrementXP(guildId, memberId, amount, options?) | string, string, number, object | Promise<{ doc, leveledUp, newLevel }> |
| enableXpCooldown() | — | void |
ModLog
| Método | Parâmetros | Retorno |
|--------|-----------|---------|
| addModLog(admin, target, reason, type) | GuildMember, GuildMember|User, string, string | Promise<doc> |
| getWarnings(guildId, targetId) | string, string | Promise<doc[]> |
| clearWarnings(guildId, targetId) | string, string | Promise<object> |
| getAllWarnings(guildId) | string | Promise<doc[]> |
| deleteWarn(guildId, warnId) | string, string | Promise<doc\|null> |
ReactionRoles
| Método | Parâmetros | Retorno |
|--------|-----------|---------|
| cacheReactionRoles(client) | Discord Client | Promise<void> |
| getReactionRoles(guildId, channelId, messageId) | string, string, string | { emote, role_id }[] |
| addReactionRole(guildId, channelId, messageId, emote, roleId) | strings | Promise<void> |
| removeReactionRole(guildId, channelId, messageId) | string, string, string | Promise<void> |
Suggestions
| Método | Parâmetros | Retorno |
|--------|-----------|---------|
| addSuggestion(message, userId, suggestion) | Message, string, string | Promise<doc> |
| findSuggestion(guildId, messageId) | string, string | Promise<doc\|null> |
| deleteSuggestion(guildId, messageId, memberId, reason) | strings | Promise<doc> |
Partnerships
| Método | Parâmetros | Retorno |
|--------|-----------|---------|
| addPartnership(messageId, guildId, data) | string, string, object | Promise<doc> |
| getPartnerships(guildId) | string | Promise<doc[]> |
| getPartnership(messageId) | string | Promise<doc\|null> |
| removePartnership(messageId, guildId) | string, string | Promise<doc\|null> |
| removePartnershipsByRep(guildId, repId) | string, string | Promise<string[]> |
| countPromoterPartnerships(guildId, promoterId) | string, string | Promise<number> |
| getPartnershipRank(guildId) | string | Promise<{ _id, total }[]> |
Starboard
| Método | Parâmetros | Retorno |
|--------|-----------|---------|
| findStarboardEntry(guildId, messageId) | string, string | Promise<doc\|null> |
| upsertStarboardEntry(guildId, messageId, channelId, starMsgId, starCount, authorId?) | strings, number | Promise<doc> |
| updateStarCount(guildId, messageId, starCount) | string, string, number | Promise<doc> |
| deleteStarboardEntry(guildId, messageId) | string, string | Promise<doc> |
SavedEmbeds
| Método | Parâmetros | Retorno |
|--------|-----------|---------|
| saveEmbed(guildId, name, embedData, componentsData) | string, string, object, array | Promise<doc> |
| loadEmbed(guildId, name) | string, string | Promise<doc\|null> |
| listEmbeds(guildId) | string | Promise<{ name, created_at }[]> |
| deleteEmbed(guildId, name) | string, string | Promise<boolean> |
AutomodLogs
| Método | Parâmetros | Retorno |
|--------|-----------|---------|
| addAutoModLog(member, content, reason, strikes) | GuildMember, string, string, number | Promise<void> |
TranslateLog
| Método | Parâmetros | Retorno |
|--------|-----------|---------|
| isTranslated(message, code) | Message, string | Promise<doc\|null> |
| logTranslation(message, code) | Message, string | Promise<doc> |
Giveaways
| Getter | Retorno |
|--------|---------|
| giveawayModel | Mongoose model (gerenciado pelo discord-giveaways) |
SCHEMA_KEYS
const { SCHEMA_KEYS } = require("enerthya.dev-pudding");
// Constantes para evitar typos:
SCHEMA_KEYS.GUILD // "Guild"
SCHEMA_KEYS.MEMBER_STATS // "MemberStats"
SCHEMA_KEYS.TRANSLATE_LOG // "TranslateLog"
// Validação antes de inicializar:
SCHEMA_KEYS.REQUIRED.forEach(key => {
if (!schemas[key]) throw new Error(`Schema obrigatório ausente: ${key}`);
});Como o bot inicializa
// src/structures/EnerthyaCore.js
const { PuddingClient } = require("enerthya.dev-pudding");
// Após conectar ao banco:
const pudding = new PuddingClient(EnerthyaCore.schemas);
pudding.enableXpCooldown();
// Exponha no client para uso nos handlers:
client.pudding = pudding;Estrutura
src/
├── index.js ← Exporta PuddingClient e SCHEMA_KEYS
├── structures/
│ └── PuddingClient.js ← Classe principal — 13 schemas cobertos
└── constants/
└── schemaKeys.js ← Constantes de chave + REQUIRED + OPTIONAL
test.js ← 64 testes — node test.js (0 falhas)Testes
node test.js
# Results: 64 passed, 0 failedTodos os schemas são testados com mocks em memória — sem Mongoose, sem conexão de banco.
