Add the source code

This commit is contained in:
OptimiDEV
2025-10-09 09:33:35 +02:00
parent 9425a34cb6
commit 30808f2124
19 changed files with 1582 additions and 130 deletions

View File

@@ -0,0 +1,31 @@
import { Client, Events, GuildBan, GuildMember, PartialGuildMember } from 'discord.js';
import { withConn } from '../db/pool.js';
async function getLogsChannelId(guildId: string): Promise<string | null> {
const row = await withConn(async (conn) => {
const rows = await conn.query('SELECT logs_enabled, logs_channel_id FROM guild_config WHERE guild_id = ? LIMIT 1', [guildId]);
return rows[0] as { logs_enabled?: number, logs_channel_id?: string } | undefined;
});
if (!row?.logs_enabled || !row.logs_channel_id) return null;
return row.logs_channel_id;
}
export function registerModerationLogs(client: Client) {
client.on(Events.GuildBanAdd, async (ban: GuildBan) => {
const channelId = await getLogsChannelId(ban.guild.id);
if (!channelId) return;
const channel = ban.guild.channels.cache.get(channelId);
if (!channel || !('send' in channel)) return;
await (channel as any).send(`User banned: <@${ban.user.id}>`);
});
client.on(Events.GuildMemberRemove, async (member: GuildMember | PartialGuildMember) => {
const channelId = await getLogsChannelId(member.guild.id);
if (!channelId) return;
const channel = member.guild.channels.cache.get(channelId);
if (!channel || !('send' in channel)) return;
await (channel as any).send(`Member left or was kicked: <@${member.id}>`);
});
}

View File

@@ -0,0 +1,41 @@
import { Client, Events, Message } from 'discord.js';
import { withConn } from '../db/pool.js';
import { chatAnswerWithMemory } from '../ai/gemini.js';
import filter from 'leo-profanity';
filter.loadDictionary('en');
export function registerMessageCreate(client: Client) {
client.on(Events.MessageCreate, async (message: Message) => {
if (!message.guild || message.author.bot) return;
const guildId = message.guild!.id;
const cfg = await withConn(async (conn) => {
const rows = await conn.query('SELECT ai_enabled, profanity_filter_enabled FROM guild_config WHERE guild_id = ? LIMIT 1', [guildId]);
return rows[0] as { ai_enabled?: number, profanity_filter_enabled?: number } | undefined;
});
// Profanity filter
if (cfg?.profanity_filter_enabled) {
if (filter.check(message.content)) {
await message.delete().catch(() => null);
return;
}
}
// XP system: 10 XP per message
await withConn(async (conn) => {
await conn.query('INSERT INTO user_xp (guild_id, user_id, xp) VALUES (?, ?, 10) ON DUPLICATE KEY UPDATE xp = xp + 10', [guildId, message.author.id]);
});
// Mention AI
if (cfg?.ai_enabled && message.mentions.has(client.user!)) {
const question = message.content.replace(/<@!?\d+>/g, '').trim();
if (!question) return;
const answer = await chatAnswerWithMemory(guildId, message.author.id, question).catch(() => 'Sorry, I could not process that right now.');
await message.reply(answer);
}
});
}