swapped to a new db locally hosted

This commit is contained in:
2025-10-06 00:25:29 -04:00
parent 097583ca0a
commit ca23c0ab8c
40 changed files with 2244 additions and 556 deletions

View File

@@ -0,0 +1,33 @@
const { SlashCommandBuilder, PermissionsBitField } = require('discord.js');
const fetch = require('node-fetch');
module.exports = {
name: 'add-twitchuser',
description: 'Admin: add a Twitch username to watch for this server',
enabled: true,
builder: new SlashCommandBuilder()
.setName('add-twitchuser')
.setDescription('Add a Twitch username to watch for live notifications')
.addStringOption(opt => opt.setName('username').setDescription('Twitch username').setRequired(true)),
async execute(interaction) {
if (!interaction.member.permissions.has(PermissionsBitField.Flags.Administrator)) {
await interaction.reply({ content: 'You must be an administrator to use this command.', flags: 64 });
return;
}
const username = interaction.options.getString('username').toLowerCase().trim();
try {
const backendBase = process.env.BACKEND_BASE || `http://${process.env.HOST || '127.0.0.1'}:${process.env.PORT || 3002}`;
const resp = await fetch(`${backendBase}/api/servers/${interaction.guildId}/twitch-users`, {
method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username })
});
if (resp.ok) {
await interaction.reply({ content: `Added ${username} to watch list.`, flags: 64 });
} else {
await interaction.reply({ content: 'Failed to add user via backend.', flags: 64 });
}
} catch (e) {
console.error('Error adding twitch user:', e);
await interaction.reply({ content: 'Internal error adding twitch user.', flags: 64 });
}
}
};

View File

@@ -28,22 +28,21 @@ module.exports = {
const invite = await targetChannel.createInvite({ maxAge, maxUses, temporary, unique: true });
const db = readDb();
if (!db[interaction.guildId]) db[interaction.guildId] = {};
if (!db[interaction.guildId].invites) db[interaction.guildId].invites = [];
const api = require('../api');
const item = {
code: invite.code,
url: invite.url,
channelId: targetChannel.id,
createdAt: new Date().toISOString(),
maxUses: invite.maxUses || maxUses || 0,
maxAge: invite.maxAge || maxAge || 0,
channel_id: targetChannel.id,
created_at: new Date().toISOString(),
max_uses: invite.maxUses || maxUses || 0,
max_age: invite.maxAge || maxAge || 0,
temporary: !!invite.temporary,
};
db[interaction.guildId].invites.push(item);
writeDb(db);
try {
await api.addInvite(interaction.guildId, { channelId: targetChannel.id, maxAge, maxUses, temporary });
} catch (e) {
console.error('Error saving invite to backend:', e);
}
await interaction.reply({ content: `Invite created: ${invite.url}`, ephemeral: true });
} catch (error) {

View File

@@ -1,4 +1,4 @@
const { SlashCommandBuilder } = require('discord.js');
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
module.exports = {
name: 'help',
@@ -6,20 +6,62 @@ module.exports = {
enabled: true,
builder: new SlashCommandBuilder()
.setName('help')
.setDescription('List available bot commands and what they do.'),
.setDescription('List available bot commands and what they do.')
.addStringOption(opt => opt.setName('command').setDescription('Get detailed help for a specific command').setRequired(false)),
async execute(interaction) {
const commands = Array.from(interaction.client.commands.values()).filter(cmd => !!cmd.builder);
let text = '**Available Commands:**\n\n';
const db = require('../../backend/db').readDb();
const guildSettings = db[interaction.guildId] || {};
const toggles = guildSettings.commandToggles || {};
const protectedCommands = ['manage-commands', 'help'];
try {
const api = require('../api');
// fetch authoritative commands list for this guild
const commands = await api.getCommands(interaction.guildId) || [];
for (const cmd of commands) {
const isEnabled = protectedCommands.includes(cmd.name) ? true : (toggles[cmd.name] !== false && cmd.enabled !== false);
text += `/${cmd.name}${cmd.description || 'No description.'}${isEnabled ? 'Enabled' : 'Disabled'}${protectedCommands.includes(cmd.name) ? ' (locked)' : ''}\n`;
const target = interaction.options.getString('command');
if (target) {
const found = commands.find(c => c.name.toLowerCase() === target.toLowerCase());
if (!found) {
return await interaction.reply({ content: `No command named "/${target}" found.`, flags: 64 });
}
const embed = new EmbedBuilder()
.setTitle(`/${found.name}${found.locked ? 'Locked' : (found.enabled ? 'Enabled' : 'Disabled')}`)
.setDescription(found.description || 'No description available.')
.setColor(found.enabled ? 0x22c55e : 0xe11d48)
.addFields(
{ name: 'Usage', value: `/${found.name} ${(found.usage || '').trim() || ''}` },
{ name: 'Status', value: found.locked ? 'Locked (cannot be toggled)' : (found.enabled ? 'Enabled' : 'Disabled'), inline: true },
{ name: 'Has Slash Builder', value: found.hasSlashBuilder ? 'Yes' : 'No', inline: true }
)
.setFooter({ text: 'Use /help <command> to view detailed info about a command.' });
return await interaction.reply({ embeds: [embed], flags: 64 });
}
// Build a neat embed listing commands grouped by status
const embed = new EmbedBuilder()
.setTitle('Available Commands')
.setDescription('Use `/help <command>` to get detailed info on a specific command.')
.setColor(0x5865f2);
// Sort commands: enabled first, then disabled, locked last
const sorted = commands.slice().sort((a, b) => {
const ka = a.locked ? 2 : (a.enabled ? 0 : 1);
const kb = b.locked ? 2 : (b.enabled ? 0 : 1);
if (ka !== kb) return ka - kb;
return a.name.localeCompare(b.name);
});
// Build a concise field list (max 25 fields in Discord embed)
const fields = [];
for (const cmd of sorted) {
const status = cmd.locked ? '🔒 Locked' : (cmd.enabled ? '✅ Enabled' : '⛔ Disabled');
fields.push({ name: `/${cmd.name}`, value: `${cmd.description || 'No description.'}\n${status}`, inline: false });
if (fields.length >= 24) break;
}
if (fields.length > 0) embed.addFields(fields);
else embed.setDescription('No commands available.');
return await interaction.reply({ embeds: [embed], flags: 64 });
} catch (e) {
console.error('Error in help command:', e && e.message ? e.message : e);
return await interaction.reply({ content: 'Failed to retrieve commands. Try again later.', flags: 64 });
}
await interaction.reply({ content: text, flags: 64 });
},
};

View File

@@ -1,5 +1,5 @@
const { SlashCommandBuilder, PermissionFlagsBits, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require('discord.js');
const { readDb } = require('../../backend/db');
const api = require('../api');
module.exports = {
name: 'list-invites',
@@ -11,8 +11,7 @@ module.exports = {
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator),
async execute(interaction) {
try {
const db = readDb();
const invites = (db[interaction.guildId] && db[interaction.guildId].invites) ? db[interaction.guildId].invites : [];
const invites = await api.listInvites(interaction.guildId) || [];
if (!invites.length) {
await interaction.reply({ content: 'No invites created by the bot in this server.', ephemeral: true });

View File

@@ -0,0 +1,23 @@
const { SlashCommandBuilder } = require('discord.js');
const api = require('../api');
module.exports = {
name: 'list-twitchusers',
description: 'List watched Twitch usernames for this server (Live Notifications).',
enabled: true,
builder: new SlashCommandBuilder().setName('list-twitchusers').setDescription('List watched Twitch usernames for this server'),
async execute(interaction) {
try {
const users = await api.getTwitchUsers(interaction.guildId) || [];
if (!users || users.length === 0) {
await interaction.reply({ content: 'No Twitch users are being watched for this server.', ephemeral: true });
return;
}
const list = users.map(u => `${u}`).join('\n');
await interaction.reply({ content: `Watched Twitch users:\n${list}`, ephemeral: true });
} catch (e) {
console.error('Error listing twitch users:', e);
await interaction.reply({ content: 'Failed to retrieve watched users.', ephemeral: true });
}
},
};

View File

@@ -1,5 +1,5 @@
const { SlashCommandBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, PermissionsBitField } = require('discord.js');
const { readDb, writeDb } = require('../../backend/db.js');
const api = require('../api');
module.exports = {
name: 'manage-commands',
@@ -15,11 +15,9 @@ module.exports = {
return;
}
const db = readDb();
if (!db[interaction.guildId]) db[interaction.guildId] = {};
if (!db[interaction.guildId].commandToggles) db[interaction.guildId].commandToggles = {};
const toggles = db[interaction.guildId].commandToggles;
const existingSettings = (await api.getServerSettings(interaction.guildId)) || {};
if (!existingSettings.commandToggles) existingSettings.commandToggles = {};
let toggles = existingSettings.commandToggles;
// Include all loaded commands so simple command modules (no SlashCommandBuilder) like
// `ping` are also listed. Filter for objects with a name for safety.
const commands = Array.from(interaction.client.commands.values()).filter(cmd => cmd && cmd.name);
@@ -67,9 +65,19 @@ module.exports = {
collector.on('collect', async i => {
const cmdName = i.customId.replace('toggle_cmd_', '');
toggles[cmdName] = !(toggles[cmdName] !== false);
writeDb(db);
const newVal = !(toggles[cmdName] !== false);
// persist via backend API
try {
await api.toggleCommand(interaction.guildId, cmdName, newVal);
// fetch authoritative list to rebuild buttons
const fresh = await api.getCommands(interaction.guildId);
toggles = {};
for (const c of fresh) {
toggles[c.name] = c.enabled;
}
} catch (e) {
console.error('Error persisting command toggle:', e);
}
// rebuild buttons to reflect new state
const updatedRows = [];
let r = new ActionRowBuilder();

View File

@@ -1,17 +1,10 @@
const { readDb } = require('../../backend/db.js');
// ping uses backend settings via API
module.exports = {
name: 'ping',
description: 'Replies with Pong!',
enabled: true,
execute(interaction) {
const db = readDb();
const settings = db[interaction.guildId] || { pingCommand: false };
if (settings.pingCommand) {
interaction.reply('Pong!');
} else {
interaction.reply('The ping command is disabled on this server.');
}
async execute(interaction) {
await interaction.reply('Pong!');
},
};

View File

@@ -0,0 +1,31 @@
const { SlashCommandBuilder, PermissionsBitField } = require('discord.js');
const fetch = require('node-fetch');
module.exports = {
name: 'remove-twitchuser',
description: 'Admin: remove a Twitch username from this server watch list',
enabled: true,
builder: new SlashCommandBuilder()
.setName('remove-twitchuser')
.setDescription('Remove a Twitch username from the watch list')
.addStringOption(opt => opt.setName('username').setDescription('Twitch username to remove').setRequired(true)),
async execute(interaction) {
if (!interaction.member.permissions.has(PermissionsBitField.Flags.Administrator)) {
await interaction.reply({ content: 'You must be an administrator to use this command.', flags: 64 });
return;
}
const username = interaction.options.getString('username').toLowerCase().trim();
try {
const backendBase = process.env.BACKEND_BASE || `http://${process.env.HOST || '127.0.0.1'}:${process.env.PORT || 3002}`;
const resp = await fetch(`${backendBase}/api/servers/${interaction.guildId}/twitch-users/${encodeURIComponent(username)}`, { method: 'DELETE' });
if (resp.ok) {
await interaction.reply({ content: `Removed ${username} from watch list.`, flags: 64 });
} else {
await interaction.reply({ content: 'Failed to remove user via backend.', flags: 64 });
}
} catch (e) {
console.error('Error removing twitch user:', e);
await interaction.reply({ content: 'Internal error removing twitch user.', flags: 64 });
}
}
};

View File

@@ -9,13 +9,8 @@ module.exports = {
.setName('setup-autorole')
.setDescription('Interactively set up the autorole for this server.'),
async execute(interaction) {
const db = readDb();
const guildId = interaction.guildId;
if (!db[guildId]) {
db[guildId] = {};
}
const roleSelect = new RoleSelectMenuBuilder()
.setCustomId('autorole_role_select')
.setPlaceholder('Select the role to assign on join.');
@@ -45,11 +40,20 @@ module.exports = {
return;
}
db[guildId].autorole = {
enabled: true,
roleId: roleId,
};
writeDb(db);
// persist to backend
try {
const api = require('../api');
const existing = await api.getServerSettings(guildId) || {};
existing.autorole = { enabled: true, roleId };
await api.upsertServerSettings(guildId, existing);
} catch (e) {
console.error('Error persisting autorole to backend, falling back to local:', e);
const { readDb, writeDb } = require('../../backend/db.js');
const db = readDb();
if (!db[guildId]) db[guildId] = {};
db[guildId].autorole = { enabled: true, roleId };
writeDb(db);
}
await roleConfirmation.update({
content: `Autorole setup complete! New members will be assigned the **${role.name}** role.`,

View File

@@ -1,4 +1,5 @@
const { SlashCommandBuilder, ActionRowBuilder, ChannelSelectMenuBuilder, StringSelectMenuBuilder, ModalBuilder, TextInputBuilder, TextInputStyle, ComponentType } = require('discord.js');
const api = require('../api');
const { readDb, writeDb } = require('../../backend/db.js');
const defaultLeaveMessages = ["{user} has left the server.", "Goodbye, {user}.", "We'll miss you, {user}."];
@@ -38,8 +39,19 @@ module.exports = {
});
const channelId = channelConfirmation.values[0];
db[guildId].leaveChannel = channelId;
db[guildId].leaveEnabled = true;
try {
const existing = (await api.getServerSettings(guildId)) || {};
existing.leaveEnabled = true;
existing.leaveChannel = channelId;
await api.upsertServerSettings(guildId, existing);
} catch (e) {
console.error('Error persisting leave settings to backend, falling back to local:', e);
const db = readDb();
if (!db[guildId]) db[guildId] = {};
db[guildId].leaveChannel = channelId;
db[guildId].leaveEnabled = true;
writeDb(db);
}
const messageOptions = defaultLeaveMessages.map(msg => ({
label: msg.length > 100 ? msg.substring(0, 97) + '...' : msg,
@@ -92,8 +104,17 @@ module.exports = {
});
const customMessage = modalSubmit.fields.getTextInputValue('custom_message_input');
db[guildId].leaveMessage = customMessage;
writeDb(db);
try {
const existing = (await api.getServerSettings(guildId)) || {};
existing.leaveMessage = customMessage;
await api.upsertServerSettings(guildId, existing);
} catch (e) {
console.error('Error persisting leave message to backend, falling back to local:', e);
const db = readDb();
if (!db[guildId]) db[guildId] = {};
db[guildId].leaveMessage = customMessage;
writeDb(db);
}
await modalSubmit.reply({
content: `Leave message setup complete! Channel: <#${channelId}>, Message: "${customMessage}"`,
@@ -101,8 +122,17 @@ module.exports = {
});
} else {
db[guildId].leaveMessage = selectedMessage;
writeDb(db);
try {
const existing = (await api.getServerSettings(guildId)) || {};
existing.leaveMessage = selectedMessage;
await api.upsertServerSettings(guildId, existing);
} catch (e) {
console.error('Error persisting leave message to backend, falling back to local:', e);
const db = readDb();
if (!db[guildId]) db[guildId] = {};
db[guildId].leaveMessage = selectedMessage;
writeDb(db);
}
await messageConfirmation.update({
content: `Leave message setup complete! Channel: <#${channelId}>, Message: "${selectedMessage}"`,
components: [],

View File

@@ -0,0 +1,43 @@
const { SlashCommandBuilder, PermissionsBitField } = require('discord.js');
const fetch = require('node-fetch');
const api = require('../api');
const { readDb, writeDb } = require('../../backend/db');
module.exports = {
name: 'setup-live',
description: 'Admin: configure Twitch live notifications for this server',
enabled: true,
builder: new SlashCommandBuilder()
.setName('setup-live')
.setDescription('Configure Twitch live notifications for this server')
.addStringOption(opt => opt.setName('twitch_user').setDescription('Twitch username to watch').setRequired(true))
.addChannelOption(opt => opt.setName('channel').setDescription('Channel to send notifications').setRequired(true))
.addBooleanOption(opt => opt.setName('enabled').setDescription('Enable/disable notifications').setRequired(true)),
async execute(interaction) {
if (!interaction.member.permissions.has(PermissionsBitField.Flags.Administrator)) {
await interaction.reply({ content: 'You must be a server administrator to configure live notifications.', flags: 64 });
return;
}
const twitchUser = interaction.options.getString('twitch_user');
const channel = interaction.options.getChannel('channel');
const enabled = interaction.options.getBoolean('enabled');
try {
const api = require('../api');
const existing = (await api.getServerSettings(interaction.guildId)) || {};
existing.liveNotifications = { enabled: !!enabled, twitchUser, channelId: channel.id };
await api.upsertServerSettings(interaction.guildId, existing);
await interaction.reply({ content: `Live notifications ${enabled ? 'enabled' : 'disabled'} for ${twitchUser} -> ${channel.name}`, flags: 64 });
} catch (e) {
console.error('Error saving live notifications to backend, falling back to local:', e);
// fallback to local db
const db = readDb();
if (!db[interaction.guildId]) db[interaction.guildId] = {};
db[interaction.guildId].liveNotifications = { enabled, twitchUser, channelId: channel.id };
writeDb(db);
await interaction.reply({ content: `Saved locally: Live notifications ${enabled ? 'enabled' : 'disabled'} for ${twitchUser} -> ${channel.name}`, flags: 64 });
}
}
};

View File

@@ -1,4 +1,5 @@
const { SlashCommandBuilder, ActionRowBuilder, ChannelSelectMenuBuilder, StringSelectMenuBuilder, ModalBuilder, TextInputBuilder, TextInputStyle, ComponentType } = require('discord.js');
const api = require('../api');
const { readDb, writeDb } = require('../../backend/db.js');
const defaultWelcomeMessages = ["Welcome to the server, {user}!", "Hey {user}, welcome!", "{user} has joined the party!"];
@@ -38,8 +39,20 @@ module.exports = {
});
const channelId = channelConfirmation.values[0];
db[guildId].welcomeChannel = channelId;
db[guildId].welcomeEnabled = true;
// persist via backend
try {
const existing = (await api.getServerSettings(guildId)) || {};
existing.welcomeEnabled = true;
existing.welcomeChannel = channelId;
await api.upsertServerSettings(guildId, existing);
} catch (e) {
console.error('Error persisting welcome settings to backend, falling back to local:', e);
const db = readDb();
if (!db[guildId]) db[guildId] = {};
db[guildId].welcomeChannel = channelId;
db[guildId].welcomeEnabled = true;
writeDb(db);
}
const messageOptions = defaultWelcomeMessages.map(msg => ({
label: msg.length > 100 ? msg.substring(0, 97) + '...' : msg,
@@ -92,8 +105,17 @@ module.exports = {
});
const customMessage = modalSubmit.fields.getTextInputValue('custom_message_input');
db[guildId].welcomeMessage = customMessage;
writeDb(db);
try {
const existing = (await api.getServerSettings(guildId)) || {};
existing.welcomeMessage = customMessage;
await api.upsertServerSettings(guildId, existing);
} catch (e) {
console.error('Error persisting welcome message to backend, falling back to local:', e);
const db = readDb();
if (!db[guildId]) db[guildId] = {};
db[guildId].welcomeMessage = customMessage;
writeDb(db);
}
await modalSubmit.reply({
content: `Welcome message setup complete! Channel: <#${channelId}>, Message: "${customMessage}"`,
@@ -101,8 +123,17 @@ module.exports = {
});
} else {
db[guildId].welcomeMessage = selectedMessage;
writeDb(db);
try {
const existing = (await api.getServerSettings(guildId)) || {};
existing.welcomeMessage = selectedMessage;
await api.upsertServerSettings(guildId, existing);
} catch (e) {
console.error('Error persisting welcome message to backend, falling back to local:', e);
const db = readDb();
if (!db[guildId]) db[guildId] = {};
db[guildId].welcomeMessage = selectedMessage;
writeDb(db);
}
await messageConfirmation.update({
content: `Welcome message setup complete! Channel: <#${channelId}>, Message: "${selectedMessage}"`,
components: [],

View File

@@ -1,5 +1,5 @@
const { SlashCommandBuilder } = require('discord.js');
const { readDb } = require('../../backend/db.js');
const api = require('../api');
module.exports = {
name: 'view-autorole',
@@ -9,10 +9,9 @@ module.exports = {
.setName('view-autorole')
.setDescription('View the current autorole configuration for this server.'),
async execute(interaction) {
const db = readDb();
const guildId = interaction.guildId;
const settings = db[guildId] || {};
const autorole = settings.autorole || { enabled: false, roleId: '' };
const guildId = interaction.guildId;
const settings = (await api.getServerSettings(guildId)) || {};
const autorole = settings.autorole || { enabled: false, roleId: '' };
if (!autorole.enabled) {
await interaction.reply({ content: 'Autorole is currently disabled for this server.', flags: 64 });

View File

@@ -1,5 +1,5 @@
const { SlashCommandBuilder } = require('discord.js');
const { readDb } = require('../../backend/db.js');
const api = require('../api');
module.exports = {
name: 'view-welcome-leave',
@@ -9,9 +9,8 @@ module.exports = {
.setName('view-welcome-leave')
.setDescription('View the current welcome and leave message configuration.'),
async execute(interaction) {
const db = readDb();
const guildId = interaction.guildId;
const settings = db[guildId] || {};
const guildId = interaction.guildId;
const settings = (await api.getServerSettings(guildId)) || {};
const welcomeChannel = settings.welcomeChannel ? `<#${settings.welcomeChannel}>` : 'Not set';
const welcomeMessage = settings.welcomeMessage || 'Not set';