Files
ECS-FullStack/discord-bot/commands/setup-live.js

44 lines
2.2 KiB
JavaScript

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 });
}
}
};