Add Kick streaming monitoring module
All checks were successful
Auto Build and Push Docker Image / build (push) Successful in 36s
All checks were successful
Auto Build and Push Docker Image / build (push) Successful in 36s
This commit is contained in:
170
src/commands/utility/kickstream.ts
Normal file
170
src/commands/utility/kickstream.ts
Normal file
@@ -0,0 +1,170 @@
|
||||
import { SlashCommandBuilder, PermissionFlagsBits, EmbedBuilder, MessageFlags, ChannelType } from 'discord.js';
|
||||
import { Command } from '../../structures/Command.js';
|
||||
import { KickManager } from '../../structures/KickManager.js';
|
||||
|
||||
const command: Command = {
|
||||
data: new SlashCommandBuilder()
|
||||
.setName('kickstream')
|
||||
.setDescription('Kick-Streamer Benachrichtigungen verwalten.')
|
||||
.addSubcommand(subcommand =>
|
||||
subcommand
|
||||
.setName('add')
|
||||
.setDescription('Fügt einen Kick-Streamer zum Monitoring hinzu.')
|
||||
.addStringOption(option =>
|
||||
option.setName('channel')
|
||||
.setDescription('Der Kick-Channel-Name (Slug)')
|
||||
.setRequired(true))
|
||||
.addChannelOption(option =>
|
||||
option.setName('discord_channel')
|
||||
.setDescription('Der Discord-Kanal für Benachrichtigungen')
|
||||
.addChannelTypes(ChannelType.GuildText)
|
||||
.setRequired(true))
|
||||
.addStringOption(option =>
|
||||
option.setName('nachricht')
|
||||
.setDescription('Optionale custom Nachricht')))
|
||||
.addSubcommand(subcommand =>
|
||||
subcommand
|
||||
.setName('remove')
|
||||
.setDescription('Entfernt einen Kick-Streamer vom Monitoring.')
|
||||
.addStringOption(option =>
|
||||
option.setName('channel')
|
||||
.setDescription('Der Kick-Channel-Name')
|
||||
.setRequired(true)))
|
||||
.addSubcommand(subcommand =>
|
||||
subcommand
|
||||
.setName('list')
|
||||
.setDescription('Listet alle überwachten Kick-Streamer auf.'))
|
||||
.addSubcommand(subcommand =>
|
||||
subcommand
|
||||
.setName('online')
|
||||
.setDescription('Prüft ob ein Kick-Streamer online ist.')
|
||||
.addStringOption(option =>
|
||||
option.setName('channel')
|
||||
.setDescription('Der Kick-Channel-Name')
|
||||
.setRequired(true)))
|
||||
.addSubcommand(subcommand =>
|
||||
subcommand
|
||||
.setName('help')
|
||||
.setDescription('Zeigt Hilfe zu den Kick-Befehlen an.')),
|
||||
category: 'Admin',
|
||||
async execute(interaction: any) {
|
||||
const subcommand = interaction.options.getSubcommand();
|
||||
const guildId = interaction.guildId;
|
||||
const isAdmin = interaction.memberPermissions?.has(PermissionFlagsBits.ModerateMembers);
|
||||
const DB = interaction.client.DB;
|
||||
|
||||
if (subcommand === 'help') {
|
||||
const embed = new EmbedBuilder()
|
||||
.setTitle('🟢 Kick Streaming Hilfe')
|
||||
.setColor(0x53fc18)
|
||||
.setDescription('Benachrichtigungen bei Kick-Streams.')
|
||||
.addFields(
|
||||
{ name: '`/kickstream add <channel> <discord_channel> [nachricht]`', value: 'Fügt einen Streamer hinzu (Mod).', inline: false },
|
||||
{ name: '`/kickstream remove <channel>`', value: 'Entfernt einen Streamer (Mod).', inline: false },
|
||||
{ name: '`/kickstream list`', value: 'Zeigt alle überwachten Streamer.', inline: false },
|
||||
{ name: '`/kickstream online <channel>`', value: 'Prüft ob ein Streamer live ist.', inline: false },
|
||||
{ name: 'Hinweis', value: 'Benötigt KICK_CLIENT_ID und KICK_CLIENT_SECRET in der .env', inline: false }
|
||||
)
|
||||
.setTimestamp();
|
||||
|
||||
await interaction.reply({ embeds: [embed], flags: [MessageFlags.Ephemeral] });
|
||||
return;
|
||||
}
|
||||
|
||||
if (subcommand === 'online') {
|
||||
const channelName = interaction.options.getString('channel')!.toLowerCase().replace(/^@/, '');
|
||||
await interaction.deferReply();
|
||||
|
||||
const { channel, livestream } = await KickManager.fetchChannelData(channelName);
|
||||
if (!channel) {
|
||||
await interaction.editReply({ content: `❌ Channel **${channelName}** nicht gefunden.` });
|
||||
return;
|
||||
}
|
||||
|
||||
const isLive = livestream !== null;
|
||||
const embed = new EmbedBuilder()
|
||||
.setTitle(isLive ? `🟢 ${channel.user.username} ist LIVE` : `⚫ ${channel.user.username} ist offline`)
|
||||
.setURL(`https://kick.com/${channel.slug}`)
|
||||
.setColor(isLive ? 0x53fc18 : 0x5865F2);
|
||||
|
||||
if (isLive && livestream) {
|
||||
embed.addFields(
|
||||
{ name: 'Titel', value: livestream.stream_title || 'Kein Titel', inline: false },
|
||||
{ name: 'Kategorie', value: livestream.category?.name || 'Unbekannt', inline: true },
|
||||
{ name: 'Zuschauer', value: livestream.viewer_count.toString(), inline: true }
|
||||
);
|
||||
if (livestream.thumbnail) {
|
||||
embed.setImage(livestream.thumbnail);
|
||||
}
|
||||
}
|
||||
|
||||
await interaction.editReply({ embeds: [embed] });
|
||||
return;
|
||||
}
|
||||
|
||||
if (subcommand === 'list') {
|
||||
const monitors: any[] = DB.all('SELECT * FROM kick_monitors WHERE guild_id = ? ORDER BY channel_name ASC', guildId);
|
||||
|
||||
if (monitors.length === 0) {
|
||||
await interaction.reply({ content: 'Keine Kick-Streamer überwacht.', flags: [MessageFlags.Ephemeral] });
|
||||
return;
|
||||
}
|
||||
|
||||
const embed = new EmbedBuilder()
|
||||
.setTitle('🟢 Kick-Monitoring')
|
||||
.setColor(0x53fc18)
|
||||
.setDescription(monitors.map((m: any) =>
|
||||
`• **${m.channel_name}** → <#${m.discord_channel_id}> ${m.last_status === 'online' ? '🔴' : '⚫'}`
|
||||
).join('\n'))
|
||||
.setTimestamp();
|
||||
|
||||
await interaction.reply({ embeds: [embed], flags: [MessageFlags.Ephemeral] });
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isAdmin) {
|
||||
await interaction.reply({ content: '❌ Keine Berechtigung. Moderatoren erforderlich.', flags: [MessageFlags.Ephemeral] });
|
||||
return;
|
||||
}
|
||||
|
||||
if (subcommand === 'add') {
|
||||
const channelName = interaction.options.getString('channel')!.toLowerCase().replace(/^@/, '');
|
||||
const discordChannel = interaction.options.getChannel('discord_channel');
|
||||
const customMessage = interaction.options.getString('nachricht');
|
||||
|
||||
const { channel } = await KickManager.fetchChannelData(channelName);
|
||||
if (!channel) {
|
||||
await interaction.reply({ content: `❌ Kick-Channel **${channelName}** nicht gefunden.`, flags: [MessageFlags.Ephemeral] });
|
||||
return;
|
||||
}
|
||||
|
||||
DB.run(
|
||||
'INSERT INTO kick_monitors (guild_id, channel_name, discord_channel_id, custom_message) VALUES (?, ?, ?, ?) ON CONFLICT(guild_id, channel_name) DO UPDATE SET discord_channel_id = ?, custom_message = ?',
|
||||
guildId,
|
||||
channelName,
|
||||
discordChannel.id,
|
||||
customMessage,
|
||||
discordChannel.id,
|
||||
customMessage
|
||||
);
|
||||
|
||||
await interaction.reply({ content: `✅ **${channelName}** wird jetzt überwacht.`, flags: [MessageFlags.Ephemeral] });
|
||||
return;
|
||||
}
|
||||
|
||||
if (subcommand === 'remove') {
|
||||
const channelName = interaction.options.getString('channel')!.toLowerCase().replace(/^@/, '');
|
||||
|
||||
const result = DB.run('DELETE FROM kick_monitors WHERE guild_id = ? AND channel_name = ?', guildId, channelName);
|
||||
|
||||
if (result.changes === 0) {
|
||||
await interaction.reply({ content: `❌ **${channelName}** nicht gefunden.`, flags: [MessageFlags.Ephemeral] });
|
||||
return;
|
||||
}
|
||||
|
||||
await interaction.reply({ content: `✅ **${channelName}** entfernt.`, flags: [MessageFlags.Ephemeral] });
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
export default command;
|
||||
@@ -6,6 +6,7 @@ import { dirname, join } from 'node:path';
|
||||
import { Command } from './structures/Command.js';
|
||||
import { DB } from './structures/Database.js';
|
||||
import { TwitchManager } from './structures/TwitchManager.js';
|
||||
import { KickManager } from './structures/KickManager.js';
|
||||
import { ReminderManager } from './structures/ReminderManager.js';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
@@ -60,6 +61,9 @@ for (const file of eventFiles) {
|
||||
// Start Twitch Polling
|
||||
TwitchManager.startPolling(client);
|
||||
|
||||
// Start Kick Polling
|
||||
KickManager.startPolling(client);
|
||||
|
||||
// Start Reminder Polling
|
||||
ReminderManager.startPolling(client);
|
||||
|
||||
|
||||
@@ -79,6 +79,20 @@ export class DB {
|
||||
)
|
||||
`).run();
|
||||
|
||||
// Kick Monitors
|
||||
db.prepare(`
|
||||
CREATE TABLE IF NOT EXISTS kick_monitors (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
guild_id TEXT,
|
||||
channel_name TEXT,
|
||||
discord_channel_id TEXT,
|
||||
custom_message TEXT,
|
||||
last_status TEXT DEFAULT 'offline',
|
||||
last_stream_id TEXT,
|
||||
UNIQUE(guild_id, channel_name)
|
||||
)
|
||||
`).run();
|
||||
|
||||
// Auto Responses
|
||||
db.prepare(`
|
||||
CREATE TABLE IF NOT EXISTS auto_responses (
|
||||
|
||||
142
src/structures/KickManager.ts
Normal file
142
src/structures/KickManager.ts
Normal file
@@ -0,0 +1,142 @@
|
||||
import { EmbedBuilder, TextChannel } from 'discord.js';
|
||||
import { client as KickClient } from '@nekiro/kick-api';
|
||||
|
||||
interface KickChannel {
|
||||
id: number;
|
||||
user_id: number;
|
||||
slug: string;
|
||||
user: { username: string };
|
||||
category?: { id: number; name: string };
|
||||
followers_count: number;
|
||||
}
|
||||
|
||||
interface KickLivestream {
|
||||
broadcaster_user_id: number;
|
||||
category: { id: number; name: string };
|
||||
channel_id: number;
|
||||
slug: string;
|
||||
started_at: string;
|
||||
stream_title: string;
|
||||
thumbnail: string;
|
||||
viewer_count: number;
|
||||
session_id?: string;
|
||||
}
|
||||
|
||||
export class KickManager {
|
||||
private static kickClient: InstanceType<typeof KickClient> | null = null;
|
||||
private static channelLivestreamMap: Map<string, KickLivestream> = new Map();
|
||||
|
||||
private static initClient() {
|
||||
if (this.kickClient) return this.kickClient;
|
||||
|
||||
const clientId = process.env.KICK_CLIENT_ID;
|
||||
const clientSecret = process.env.KICK_CLIENT_SECRET;
|
||||
|
||||
if (!clientId || !clientSecret) {
|
||||
console.error('[KICK] Missing KICK_CLIENT_ID or KICK_CLIENT_SECRET in .env');
|
||||
return null;
|
||||
}
|
||||
|
||||
this.kickClient = new KickClient({
|
||||
clientId,
|
||||
clientSecret,
|
||||
});
|
||||
|
||||
return this.kickClient;
|
||||
}
|
||||
|
||||
static async fetchChannelData(channelName: string): Promise<{ channel: KickChannel | null; livestream: KickLivestream | null }> {
|
||||
const client = this.initClient();
|
||||
if (!client) return { channel: null, livestream: null };
|
||||
|
||||
try {
|
||||
const channels = await client.channels.getChannels({ slug: [channelName] }) as KickChannel[];
|
||||
const channel = channels?.[0] || null;
|
||||
|
||||
if (!channel) return { channel: null, livestream: null };
|
||||
|
||||
const livestreams = await client.livestreams.getLivestreams({
|
||||
broadcaster_user_id: [channel.user_id]
|
||||
}) as KickLivestream[];
|
||||
const livestream = livestreams?.[0] || null;
|
||||
|
||||
return { channel, livestream };
|
||||
} catch (error) {
|
||||
console.error(`[KICK] Error fetching data for ${channelName}:`, error);
|
||||
return { channel: null, livestream: null };
|
||||
}
|
||||
}
|
||||
|
||||
static async checkStreams(client: any) {
|
||||
if (!this.initClient()) return;
|
||||
|
||||
const monitors: any[] = (client as any).DB?.all('SELECT * FROM kick_monitors') || [];
|
||||
if (monitors.length === 0) return;
|
||||
|
||||
for (const monitor of monitors) {
|
||||
try {
|
||||
const { channel, livestream } = await this.fetchChannelData(monitor.channel_name);
|
||||
|
||||
if (channel && livestream) {
|
||||
const streamId = livestream.session_id?.toString() || livestream.broadcaster_user_id.toString();
|
||||
const isNewStream = monitor.last_stream_id !== streamId;
|
||||
const wasOffline = monitor.last_status === 'offline';
|
||||
|
||||
if (wasOffline || (monitor.last_status === 'online' && isNewStream)) {
|
||||
await this.sendNotification(client, monitor, channel, livestream);
|
||||
(client as any).DB?.run(
|
||||
"UPDATE kick_monitors SET last_status = 'online', last_stream_id = ? WHERE id = ?",
|
||||
streamId,
|
||||
monitor.id
|
||||
);
|
||||
}
|
||||
} else {
|
||||
if (monitor.last_status === 'online') {
|
||||
(client as any).DB?.run("UPDATE kick_monitors SET last_status = 'offline', last_stream_id = NULL WHERE id = ?", monitor.id);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[KICK] Error checking stream ${monitor.channel_name}:`, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static async sendNotification(client: any, monitor: any, channel: KickChannel, livestream: KickLivestream) {
|
||||
const guild = client.guilds.cache.get(monitor.guild_id);
|
||||
if (!guild || !monitor.discord_channel_id) return;
|
||||
|
||||
const discordChannel = guild.channels.cache.get(monitor.discord_channel_id) as TextChannel;
|
||||
if (!discordChannel) return;
|
||||
|
||||
const thumbnailUrl = livestream.thumbnail || `https://kick.com/img/channel-thumbnails/${channel.id}?t=${Date.now()}`;
|
||||
|
||||
const embed = new EmbedBuilder()
|
||||
.setTitle(`${channel.user.username} ist jetzt LIVE auf Kick!`)
|
||||
.setURL(`https://kick.com/${channel.slug}`)
|
||||
.setColor(0x53fc18)
|
||||
.setImage(thumbnailUrl)
|
||||
.addFields(
|
||||
{ name: 'Titel', value: livestream.stream_title || 'Kein Titel', inline: false },
|
||||
{ name: 'Kategorie', value: livestream.category?.name || 'Unbekannt', inline: true },
|
||||
{ name: 'Zuschauer', value: livestream.viewer_count.toString(), inline: true }
|
||||
)
|
||||
.setTimestamp();
|
||||
|
||||
let content = `📢 **${channel.user.username}** ist jetzt auf Kick online!`;
|
||||
if (monitor.custom_message) {
|
||||
content += `\n\n${monitor.custom_message}`;
|
||||
}
|
||||
|
||||
await discordChannel.send({ content, embeds: [embed] });
|
||||
}
|
||||
|
||||
static startPolling(client: any) {
|
||||
if (!process.env.KICK_CLIENT_ID || !process.env.KICK_CLIENT_SECRET) {
|
||||
console.log('[KICK] Skipping polling - missing credentials.');
|
||||
return;
|
||||
}
|
||||
|
||||
setInterval(() => this.checkStreams(client), 5 * 60 * 1000);
|
||||
console.log('[KICK] Polling started (5m interval).');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user