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

166
discord-bot/api.js Normal file
View File

@@ -0,0 +1,166 @@
const fetch = require('node-fetch');
// Resolve backend candidates (env or common local addresses). We'll try them in order
// for each request so the bot can still reach the backend even when it binds to
// a specific non-loopback IP.
const envBase = process.env.BACKEND_BASE ? process.env.BACKEND_BASE.replace(/\/$/, '') : null;
const host = process.env.BACKEND_HOST || process.env.HOST || '127.0.0.1';
const port = process.env.BACKEND_PORT || process.env.PORT || '3002';
const CANDIDATES = [envBase, `http://${host}:${port}`, `http://localhost:${port}`, `http://127.0.0.1:${port}`].filter(Boolean);
async function tryFetch(url, opts = {}) {
// Try each candidate base until one responds successfully
for (const base of CANDIDATES) {
const target = `${base.replace(/\/$/, '')}${url}`;
try {
const res = await fetch(target, opts);
if (res && (res.ok || res.status === 204)) {
return res;
}
// if this candidate returned a non-ok status, log and continue trying others
console.error(`Candidate ${base} returned ${res.status} ${res.statusText} for ${target}`);
} catch (e) {
// network error for this candidate; try next
// console.debug(`Candidate ${base} failed:`, e && e.message ? e.message : e);
}
}
// none of the candidates succeeded
return null;
}
async function safeFetchJsonPath(path, opts = {}) {
const res = await tryFetch(path, opts);
if (!res) return null;
try {
return await res.json();
} catch (e) {
console.error('Failed to parse JSON from backend response:', e && e.message ? e.message : e);
return null;
}
}
async function getServerSettings(guildId) {
const path = `/api/servers/${guildId}/settings`;
const json = await safeFetchJsonPath(path);
return json || {};
}
async function upsertServerSettings(guildId, settings) {
const path = `/api/servers/${guildId}/settings`;
try {
const res = await tryFetch(path, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(settings),
});
return res && res.ok;
} catch (e) {
console.error(`Failed to upsert settings for ${guildId}:`, e && e.message ? e.message : e);
return false;
}
}
async function getCommands(guildId) {
const path = `/api/servers/${guildId}/commands`;
const json = await safeFetchJsonPath(path);
return json || [];
}
async function toggleCommand(guildId, cmdName, enabled) {
const path = `/api/servers/${guildId}/commands/${cmdName}/toggle`;
try {
const res = await tryFetch(path, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ enabled }),
});
return res && res.ok;
} catch (e) {
console.error(`Failed to toggle command ${cmdName} for ${guildId}:`, e && e.message ? e.message : e);
return false;
}
}
async function listInvites(guildId) {
const path = `/api/servers/${guildId}/invites`;
const json = await safeFetchJsonPath(path);
return json || [];
}
async function addInvite(guildId, invite) {
const path = `/api/servers/${guildId}/invites`;
try {
const res = await tryFetch(path, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(invite),
});
return res && res.ok;
} catch (e) {
console.error(`Failed to add invite for ${guildId}:`, e && e.message ? e.message : e);
return false;
}
}
async function deleteInvite(guildId, code) {
const path = `/api/servers/${guildId}/invites/${code}`;
try {
const res = await tryFetch(path, { method: 'DELETE' });
return res && res.ok;
} catch (e) {
console.error(`Failed to delete invite ${code} for ${guildId}:`, e && e.message ? e.message : e);
return false;
}
}
module.exports = { getServerSettings, upsertServerSettings, getCommands, toggleCommand, listInvites, addInvite, deleteInvite };
// Twitch users helpers
async function getTwitchUsers(guildId) {
const path = `/api/servers/${guildId}/twitch-users`;
const json = await safeFetchJsonPath(path);
return json || [];
}
async function addTwitchUser(guildId, username) {
const path = `/api/servers/${guildId}/twitch-users`;
try {
const res = await tryFetch(path, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username }),
});
return res && res.ok;
} catch (e) {
console.error(`Failed to add twitch user ${username} for ${guildId}:`, e && e.message ? e.message : e);
return false;
}
}
async function deleteTwitchUser(guildId, username) {
const path = `/api/servers/${guildId}/twitch-users/${encodeURIComponent(username)}`;
try {
const res = await tryFetch(path, { method: 'DELETE' });
return res && res.ok;
} catch (e) {
console.error(`Failed to delete twitch user ${username} for ${guildId}:`, e && e.message ? e.message : e);
return false;
}
}
// Fetch stream status via backend proxy endpoint /api/twitch/streams?users=a,b,c
async function tryFetchTwitchStreams(usersCsv) {
const path = `/api/twitch/streams?users=${encodeURIComponent(usersCsv || '')}`;
const json = await safeFetchJsonPath(path);
return json || [];
}
// Raw direct call helper (not used in most environments) — kept for legacy watcher
async function _rawGetTwitchStreams(usersCsv) {
// Try direct backend candidate first
const path = `/api/twitch/streams?users=${encodeURIComponent(usersCsv || '')}`;
const res = await tryFetch(path);
if (!res) return [];
try { return await res.json(); } catch (e) { return []; }
}
module.exports = { getServerSettings, upsertServerSettings, getCommands, toggleCommand, listInvites, addInvite, deleteInvite, getTwitchUsers, addTwitchUser, deleteTwitchUser };

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';

View File

@@ -23,16 +23,17 @@ const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_BOT_TOKEN)
const deployCommands = async (guildId) => {
try {
console.log(`Started refreshing application (/) commands for guild ${guildId}.`);
// Minimal logging: indicate a refresh is happening (no per-guild spam)
console.log('🔁 Refreshing application commands...');
await rest.put(
Routes.applicationGuildCommands(process.env.DISCORD_CLIENT_ID, guildId),
{ body: commands },
);
console.log(`Successfully reloaded application (/) commands for guild ${guildId}.`);
console.log(`✅ Reloaded application commands (${commands.length} commands)`);
} catch (error) {
console.error(error);
console.error('Failed to deploy commands:', error && error.message ? error.message : error);
}
};

View File

@@ -4,40 +4,69 @@ const { readDb } = require('../../backend/db.js');
module.exports = {
name: Events.GuildMemberAdd,
async execute(member) {
try {
const db = readDb();
const settings = db[member.guild.id];
try {
const api = require('../api');
const settings = (await api.getServerSettings(member.guild.id)) || {};
if (settings && settings.welcomeEnabled && settings.welcomeChannel) {
const channel = member.guild.channels.cache.get(settings.welcomeChannel);
if (channel) {
try {
const message = (settings.welcomeMessage || 'Welcome {user} to {server}!').replace('{user}', member.user.toString()).replace('{server}', member.guild.name);
await channel.send(message);
} catch (error) {
console.error(`Could not send welcome message to channel ${settings.welcomeChannel} in guild ${member.guild.id}:`, error);
}
}
}
const welcome = {
enabled: settings.welcomeEnabled || false,
channel: settings.welcomeChannel || '',
message: settings.welcomeMessage || 'Welcome {user} to {server}!'
};
if (settings && settings.autorole && settings.autorole.enabled && settings.autorole.roleId) {
const role = member.guild.roles.cache.get(settings.autorole.roleId);
if (role) {
try {
// Re-check that role is assignable
const botHighest = member.guild.members.me.roles.highest.position;
if (role.id === member.guild.id || role.managed || role.position >= botHighest) {
console.warn(`Autorole ${role.id} in guild ${member.guild.id} is not assignable (everyone/managed/too high). Skipping.`);
return;
if (welcome && welcome.enabled && welcome.channel) {
const channel = member.guild.channels.cache.get(welcome.channel);
if (channel) {
try {
const message = (welcome.message).replace('{user}', member.user.toString()).replace('{server}', member.guild.name);
await channel.send(message);
} catch (error) {
console.error(`Could not send welcome message to channel ${welcome.channel} in guild ${member.guild.id}:`, error);
}
await member.roles.add(role);
} catch (error) {
console.error(`Could not assign autorole in guild ${member.guild.id}:`, error);
}
}
const autorole = settings.autorole || {};
if (autorole && autorole.enabled && autorole.roleId) {
const role = member.guild.roles.cache.get(autorole.roleId);
if (role) {
try {
// Re-check that role is assignable
const botHighest = member.guild.members.me.roles.highest.position;
if (role.id === member.guild.id || role.managed || role.position >= botHighest) {
console.warn(`Autorole ${role.id} in guild ${member.guild.id} is not assignable (everyone/managed/too high). Skipping.`);
return;
}
await member.roles.add(role);
} catch (error) {
console.error(`Could not assign autorole in guild ${member.guild.id}:`, error);
}
}
}
} catch (error) {
console.error(`Error in guildMemberAdd event for guild ${member.guild.id}:`, error);
// fallback to local db
try {
const db = readDb();
const settings = db[member.guild.id];
if (settings && settings.welcomeEnabled && settings.welcomeChannel) {
const channel = member.guild.channels.cache.get(settings.welcomeChannel);
if (channel) {
try {
const message = (settings.welcomeMessage || 'Welcome {user} to {server}!').replace('{user}', member.user.toString()).replace('{server}', member.guild.name);
await channel.send(message);
} catch (innerErr) { /* ignore */ }
}
}
if (settings && settings.autorole && settings.autorole.enabled && settings.autorole.roleId) {
const role = member.guild.roles.cache.get(settings.autorole.roleId);
if (role) {
try { await member.roles.add(role); } catch (innerErr) { /* ignore */ }
}
}
} catch (inner) {
// ignore fallback errors
}
}
} catch (error) {
console.error(`Error in guildMemberAdd event for guild ${member.guild.id}:`, error);
}
},
};

View File

@@ -5,39 +5,37 @@ module.exports = {
name: Events.GuildMemberRemove,
async execute(member) {
try {
const db = readDb();
const settings = db[member.guild.id];
const api = require('../api');
const settings = (await api.getServerSettings(member.guild.id)) || {};
const leave = { enabled: settings.leaveEnabled || false, channel: settings.leaveChannel || '', message: settings.leaveMessage || '{user} has left the server.' };
if (settings && settings.leaveEnabled && settings.leaveChannel) {
let channel = member.guild.channels.cache.get(settings.leaveChannel);
if (!channel) {
if (leave && leave.enabled && leave.channel) {
const channel = member.guild.channels.cache.get(leave.channel);
if (channel) {
try {
channel = await member.guild.channels.fetch(settings.leaveChannel);
} catch (err) {
return;
}
}
if (channel && channel.isTextBased && channel.isTextBased()) {
try {
const me = member.guild.members.me;
const perms = channel.permissionsFor(me);
if (!perms || !perms.has('ViewChannel') || !perms.has('SendMessages')) {
return;
}
const userMention = member.user ? (member.user.toString ? member.user.toString() : member.user.tag) : 'A user';
const message = (settings.leaveMessage || '{user} has left the server.').replace('{user}', userMention).replace('{server}', member.guild.name);
const message = (leave.message).replace('{user}', member.user.toString());
await channel.send(message);
} catch (error) {
console.error(`Could not send leave message to channel ${settings.leaveChannel} in guild ${member.guild.id}:`, error);
console.error(`Could not send leave message to channel ${leave.channel} in guild ${member.guild.id}:`, error);
}
} else {
return;
}
}
} catch (error) {
console.error(`Error in guildMemberRemove event for guild ${member.guild.id}:`, error);
// fallback to local db
try {
const db = readDb();
const settings = db[member.guild.id];
if (settings && settings.leaveEnabled && settings.leaveChannel) {
const channel = member.guild.channels.cache.get(settings.leaveChannel);
if (channel) {
try {
const message = (settings.leaveMessage || '{user} has left the server.').replace('{user}', member.user.toString());
await channel.send(message);
} catch (innerErr) { /* ignore */ }
}
}
} catch (inner) { /* ignore */ }
}
},
};

View File

@@ -5,11 +5,15 @@ module.exports = {
name: 'clientReady',
once: true,
async execute(client) {
console.log('ECS - Full Stack Bot Online!');
const guilds = client.guilds.cache.map(guild => guild.id);
for (const guildId of guilds) {
await deployCommands(guildId);
const guildIds = client.guilds.cache.map(guild => guild.id);
if (guildIds.length > 0) {
// Deploy commands for all guilds in parallel, but only log a single summary
try {
await Promise.all(guildIds.map(id => deployCommands(id)));
console.log(`🔁 Refreshed application commands for ${guildIds.length} guild(s)`);
} catch (e) {
console.error('Error refreshing application commands:', e && e.message ? e.message : e);
}
}
const activities = [
@@ -26,5 +30,8 @@ module.exports = {
client.user.setActivity(activity.name, { type: activity.type, url: activity.url });
activityIndex = (activityIndex + 1) % activities.length;
}, 3000);
// Signal that startup is complete
console.log('✅ ECS - Full Stack Bot Online!');
},
};

View File

@@ -2,12 +2,25 @@ const { Client, GatewayIntentBits, Collection } = require('discord.js');
const fs = require('fs');
const path = require('path');
const deployCommands = require('./deploy-commands');
const { readDb } = require('../backend/db');
// legacy local db is available as a fallback in some commands via require('../../backend/db')
const api = require('./api');
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers] });
client.commands = new Collection();
// In-memory cache of server settings to allow backend to push updates and make toggles immediate
const guildSettingsCache = new Map();
function setGuildSettings(guildId, settings) {
if (!guildId) return;
guildSettingsCache.set(guildId, settings || {});
}
function getGuildSettingsFromCache(guildId) {
return guildSettingsCache.get(guildId) || null;
}
const commandHandler = require('./handlers/command-handler');
const eventHandler = require('./handlers/event-handler');
@@ -20,12 +33,16 @@ client.on('interactionCreate', async interaction => {
const id = interaction.customId || '';
if (id.startsWith('copy_inv_')) {
const code = id.replace('copy_inv_', '');
const db = readDb();
const invites = (db[interaction.guildId] && db[interaction.guildId].invites) ? db[interaction.guildId].invites : [];
const inv = invites.find(i => i.code === code);
if (inv) {
await interaction.reply({ content: `Invite: ${inv.url}`, ephemeral: true });
} else {
try {
const invites = await api.listInvites(interaction.guildId);
const inv = (invites || []).find(i => i.code === code);
if (inv) {
await interaction.reply({ content: `Invite: ${inv.url}`, ephemeral: true });
} else {
await interaction.reply({ content: 'Invite not found.', ephemeral: true });
}
} catch (e) {
console.error('Error fetching invites via API:', e);
await interaction.reply({ content: 'Invite not found.', ephemeral: true });
}
} else if (id.startsWith('delete_inv_')) {
@@ -37,12 +54,13 @@ client.on('interactionCreate', async interaction => {
return;
}
try {
// call backend delete endpoint
const fetch = require('node-fetch');
const backendBase = process.env.BACKEND_BASE || `http://${process.env.HOST || '127.0.0.1'}:${process.env.PORT || 3002}`;
const url = `${backendBase}/api/servers/${interaction.guildId}/invites/${code}`;
await fetch(url, { method: 'DELETE' });
await interaction.reply({ content: 'Invite deleted.', ephemeral: true });
// call backend delete endpoint via helper
const ok = await api.deleteInvite(interaction.guildId, code);
if (ok) {
await interaction.reply({ content: 'Invite deleted.', ephemeral: true });
} else {
await interaction.reply({ content: 'Failed to delete invite via API.', ephemeral: true });
}
} catch (e) {
console.error('Error deleting invite via API:', e);
await interaction.reply({ content: 'Failed to delete invite.', ephemeral: true });
@@ -57,11 +75,28 @@ client.on('interactionCreate', async interaction => {
if (!command) return;
// Check per-guild toggles
// Check per-guild toggles via Postgres (directly) for lower latency and reliability
try {
const db = readDb();
const guildSettings = db[interaction.guildId] || {};
const toggles = guildSettings.commandToggles || {};
// authoritative path: always try the backend HTTP API first so separate processes stay in sync
let guildSettings = await api.getServerSettings(interaction.guildId) || {};
// if API didn't return anything useful, try in-memory cache then direct DB as fallbacks
if (!guildSettings || Object.keys(guildSettings).length === 0) {
guildSettings = getGuildSettingsFromCache(interaction.guildId) || {};
if (!guildSettings || Object.keys(guildSettings).length === 0) {
try {
const pg = require('../backend/pg');
guildSettings = (await pg.getServerSettings(interaction.guildId)) || {};
} catch (pgErr) {
// leave guildSettings empty
}
}
}
// Normalize legacy flags into commandToggles for backward compatibility
const toggles = { ...(guildSettings.commandToggles || {}) };
// Example legacy flag mapping: pingCommand -> commandToggles.ping
if (typeof guildSettings.pingCommand !== 'undefined') {
toggles.ping = !!guildSettings.pingCommand;
}
const protectedCommands = ['manage-commands', 'help'];
// If command is protected, always allow
@@ -97,4 +132,82 @@ const login = () => {
client.login(process.env.DISCORD_BOT_TOKEN);
}
module.exports = { login, client };
// Allow backend to trigger a live announcement for debugging
async function announceLive(guildId, stream) {
try {
const guild = await client.guilds.fetch(guildId).catch(() => null);
if (!guild) return { success: false, message: 'Guild not found' };
const settings = await (require('../backend/pg')).getServerSettings(guildId).catch(() => null) || await (require('./api')).getServerSettings(guildId).catch(() => ({}));
const liveSettings = (settings && settings.liveNotifications) || {};
if (!liveSettings.enabled) return { success: false, message: 'Live notifications disabled' };
const channelId = liveSettings.channelId;
if (!channelId) return { success: false, message: 'No channel configured' };
const channel = await guild.channels.fetch(channelId).catch(() => null);
if (!channel) return { success: false, message: 'Channel not found' };
const { EmbedBuilder } = require('discord.js');
const embed = new EmbedBuilder()
.setColor(0x9146FF)
.setTitle(stream.title || `${stream.user_name} is live`)
.setURL(stream.url)
.setAuthor({ name: stream.user_name, iconURL: stream.profile_image_url || undefined, url: stream.url })
.setThumbnail(stream.thumbnail_url || stream.profile_image_url || undefined)
.addFields(
{ name: 'Category', value: stream.game_name || 'Unknown', inline: true },
{ name: 'Viewers', value: String(stream.viewer_count || 0), inline: true }
)
.setDescription(stream.description || '')
.setFooter({ text: `ehchadservices • Started: ${stream.started_at ? new Date(stream.started_at).toLocaleString() : 'unknown'}` });
await channel.send({ embeds: [embed] });
return { success: true };
} catch (e) {
console.error('announceLive failed:', e && e.message ? e.message : e);
return { success: false, message: e && e.message ? e.message : 'unknown error' };
}
}
module.exports = { login, client, setGuildSettings, getGuildSettingsFromCache, announceLive };
// Start twitch watcher when client is ready (use 'clientReady' as the event name)
try {
const watcher = require('./twitch-watcher');
// discord.js renamed the ready event to clientReady; the event loader registers
// handlers based on event.name so we listen for the same 'clientReady' here.
client.once('clientReady', () => {
// start polling in background
watcher.poll(client).catch(err => console.error('Twitch watcher failed to start:', err));
});
process.on('exit', () => { watcher.stop(); });
process.on('SIGINT', () => { watcher.stop(); process.exit(); });
} catch (e) {
// ignore if watcher not available
}
// --- Optional push receiver (so backend can notify a remote bot process) ---
try {
const express = require('express');
const bodyParser = require('body-parser');
const botPort = process.env.BOT_PUSH_PORT || process.env.BOT_PORT || null;
const botSecret = process.env.BOT_SECRET || null;
if (botPort) {
const app = express();
app.use(bodyParser.json());
app.post('/internal/set-settings', (req, res) => {
try {
if (botSecret) {
const provided = req.headers['x-bot-secret'];
if (!provided || provided !== botSecret) return res.status(401).json({ success: false, message: 'Unauthorized' });
}
const { guildId, settings } = req.body || {};
if (!guildId) return res.status(400).json({ success: false, message: 'Missing guildId' });
setGuildSettings(guildId, settings || {});
return res.json({ success: true });
} catch (e) {
console.error('Error in bot push receiver:', e);
return res.status(500).json({ success: false });
}
});
app.listen(botPort, () => console.log(`Bot push receiver listening on port ${botPort}`));
}
} catch (e) {
// ignore if express isn't available in this environment
}

View File

@@ -11,7 +11,8 @@
"dependencies": {
"crypto-js": "^4.2.0",
"discord.js": "^14.22.1",
"dotenv": "^16.4.5"
"dotenv": "^16.4.5",
"node-fetch": "^2.6.7"
}
},
"node_modules/@discordjs/builders": {
@@ -314,6 +315,32 @@
"integrity": "sha512-ThQLOhN86ZkJ7qemtVRGYM+gRgR8GEXNli9H/PMvpnZsE44Xfh3wx9kGJaldg314v85m+bFW6WBMaVHJc/c3zA==",
"license": "MIT"
},
"node_modules/node-fetch": {
"version": "2.7.0",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz",
"integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==",
"license": "MIT",
"dependencies": {
"whatwg-url": "^5.0.0"
},
"engines": {
"node": "4.x || >=6.0.0"
},
"peerDependencies": {
"encoding": "^0.1.0"
},
"peerDependenciesMeta": {
"encoding": {
"optional": true
}
}
},
"node_modules/tr46": {
"version": "0.0.3",
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
"integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==",
"license": "MIT"
},
"node_modules/ts-mixer": {
"version": "6.0.4",
"resolved": "https://registry.npmjs.org/ts-mixer/-/ts-mixer-6.0.4.tgz",
@@ -341,6 +368,22 @@
"integrity": "sha512-Ov2Rr9Sx+fRgagJ5AX0qvItZG/JKKoBRAVITs1zk7IqZGTJUwgUr7qoYBpWwakpWilTZFM98rG/AFRocu10iIQ==",
"license": "MIT"
},
"node_modules/webidl-conversions": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
"integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==",
"license": "BSD-2-Clause"
},
"node_modules/whatwg-url": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",
"integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==",
"license": "MIT",
"dependencies": {
"tr46": "~0.0.3",
"webidl-conversions": "^3.0.0"
}
},
"node_modules/ws": {
"version": "8.18.3",
"resolved": "https://registry.npmjs.org/ws/-/ws-8.18.3.tgz",

View File

@@ -13,5 +13,6 @@
"crypto-js": "^4.2.0",
"discord.js": "^14.22.1",
"dotenv": "^16.4.5"
,"node-fetch": "^2.6.7"
}
}

View File

@@ -0,0 +1,126 @@
const api = require('./api');
let polling = false;
const pollIntervalMs = Number(process.env.TWITCH_POLL_INTERVAL_MS || 5000); // 5s default
// Keep track of which streams we've already announced per guild:user -> { started_at }
const announced = new Map(); // key: `${guildId}:${user}` -> { started_at }
async function checkGuild(client, guild) {
try {
// Intentionally quiet: per-guild checking logs are suppressed to avoid spam
const settings = await api.getServerSettings(guild.id) || {};
const liveSettings = settings.liveNotifications || {};
if (!liveSettings.enabled) return;
const channelId = liveSettings.channelId;
const users = (liveSettings.users || []).map(u => u.toLowerCase()).filter(Boolean);
if (!channelId || users.length === 0) return;
// ask backend for current live streams
const query = users.join(',');
const streams = await api._rawGetTwitchStreams ? api._rawGetTwitchStreams(query) : null;
// If the helper isn't available, try backend proxy
let live = [];
if (streams) live = streams.filter(s => s.is_live);
else {
try {
const resp = await api.tryFetchTwitchStreams(query);
live = (resp || []).filter(s => s.is_live);
} catch (e) {
live = [];
}
}
if (!live || live.length === 0) {
// No live streams: ensure any announced keys for these users are cleared so they can be re-announced later
for (const u of users) {
const key = `${guild.id}:${u}`;
if (announced.has(key)) {
announced.delete(key);
}
}
return;
}
// fetch channel using client to ensure we can reach it
let channel = null;
try {
channel = await client.channels.fetch(channelId);
} catch (e) {
console.error(`TwitchWatcher: failed to fetch channel ${channelId}:`, e && e.message ? e.message : e);
channel = null;
}
if (!channel) {
// Channel not found or inaccessible; skip
return;
}
// Build a map of live logins for quick lookup
const liveLogins = new Set(live.map(s => (s.user_login || '').toLowerCase()));
// Clear announced entries for users that are no longer live
for (const u of users) {
const key = `${guild.id}:${u}`;
if (!liveLogins.has(u) && announced.has(key)) {
announced.delete(key);
}
}
// Announce each live once per live session
for (const s of live) {
const login = (s.user_login || '').toLowerCase();
const key = `${guild.id}:${login}`;
if (announced.has(key)) continue; // already announced for this live session
// mark announced for this session
announced.set(key, { started_at: s.started_at || new Date().toISOString() });
// Build and send embed
try {
// Announce without per-guild log spam
const { EmbedBuilder } = require('discord.js');
const embed = new EmbedBuilder()
.setColor(0x9146FF)
.setTitle(s.title || `${s.user_name} is live`)
.setURL(s.url)
.setAuthor({ name: s.user_name, iconURL: s.profile_image_url || undefined, url: s.url })
.setThumbnail(s.thumbnail_url || s.profile_image_url || undefined)
.addFields(
{ name: 'Category', value: s.game_name || 'Unknown', inline: true },
{ name: 'Viewers', value: String(s.viewer_count || 0), inline: true }
)
.setDescription(s.description || '')
.setFooter({ text: `ehchadservices • Started: ${s.started_at ? new Date(s.started_at).toLocaleString() : 'unknown'}` });
await channel.send({ embeds: [embed] });
console.log(`🔔 Announced live: ${login} - ${(s.title || '').slice(0, 80)}`);
} catch (e) {
console.error(`TwitchWatcher: failed to send announcement for ${login}:`, e && e.message ? e.message : e);
// fallback
const msg = `🔴 ${s.user_name} is live: **${s.title}**\nWatch: ${s.url}`;
try { await channel.send({ content: msg }); console.log('TwitchWatcher: fallback message sent'); } catch (err) { console.error('TwitchWatcher: fallback send failed:', err && err.message ? err.message : err); }
}
}
} catch (e) {
console.error('Error checking guild for live streams:', e && e.message ? e.message : e);
}
}
async function poll(client) {
if (polling) return;
polling = true;
console.log(`🔁 TwitchWatcher started, polling every ${Math.round(pollIntervalMs/1000)}s`);
while (polling) {
try {
const guilds = Array.from(client.guilds.cache.values());
for (const g of guilds) {
await checkGuild(client, g).catch(err => { console.error('TwitchWatcher: checkGuild error', err && err.message ? err.message : err); });
}
} catch (e) {
console.error('Error during twitch poll loop:', e && e.message ? e.message : e);
}
await new Promise(r => setTimeout(r, pollIntervalMs));
}
}
function stop() { polling = false; }
module.exports = { poll, stop };