Spaces:
Runtime error

File size: 5,138 Bytes
be95fdc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const { Client, GatewayIntentBits, REST, Routes, SlashCommandBuilder } = require('discord.js');
const { joinVoiceChannel, getVoiceConnection } = require('@discordjs/voice');
const cron = require('node-cron');
const express = require('express'); // Disiapkan untuk keperluan Render nanti

// ========================================================
// KONFIGURASI UTAMA (Gunakan Environment Variable untuk Hosting)
// ========================================================
const TOKEN = process.env.TOKEN || 'MTUwNzA2NjE4MTIzMDY2MTczMg.GbexTD.x_ClW1EFzsiEi87VXa8dFh2xDVl0oISz9dKD0c';
const GUILD_ID = process.env.GUILD_ID || '1507067416272965732';
const CHANNEL_ID = process.env.CHANNEL_ID || '1507067419062173719';

const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildVoiceStates,
    ]
});

// Daftarkan Slash Command (/join dan /leave)
const commands = [
    new SlashCommandBuilder().setName('join').setDescription('Menyuruh bot masuk ke voice room secara otomatis'),
    new SlashCommandBuilder().setName('leave').setDescription('Menyuruh bot keluar dari voice room'),
].map(command => command.toJSON());

const rest = new REST({ version: '10' }).setToken(TOKEN);

client.once('ready', async () => {
    console.log(`πŸ€– Mantap! Bot ${client.user.tag} sudah aktif.`);

    // Daftarkan command ke Discord secara otomatis saat bot menyala
    try {
        console.log('⏳ Sedang mendaftarkan Slash Commands...');
        await rest.put(
            Routes.applicationCommands(client.user.id),
            { body: commands },
        );
        console.log('βœ… Slash Commands berhasil didaftarkan secara Global!');
    } catch (error) {
        console.error('❌ Gagal mendaftarkan Slash Commands:', error);
    }

    // ========================================================
    // JADWAL OTOMATIS JAM 6 PAGI (KHUSUS SERVER UTAMA)
    // ========================================================
    cron.schedule('0 6 * * *', () => {
        console.log('⏰ Jam 06.00 WIB: Bot otomatis masuk ke room utama...');
        const guild = client.guilds.cache.get(GUILD_ID);
        const channel = guild.channels.cache.get(CHANNEL_ID);
        
        if (channel) {
            joinVoiceChannel({
                channelId: channel.id,
                guildId: guild.id,
                adapterCreator: guild.voiceAdapterCreator,
                selfMute: false,
                selfDeaf: true
            });
            console.log('βœ… Bot berhasil standby di room utama.');
        }
    }, {
        timezone: "Asia/Jakarta"
    });

    // JADWAL OTOMATIS JAM 5 SORE (KHUSUS SERVER UTAMA)
    cron.schedule('0 17 * * *', () => {
        console.log('⏰ Jam 17.00 WIB: Waktunya bot keluar dari room utama...');
        const connection = getVoiceConnection(GUILD_ID);
        if (connection) {
            connection.destroy();
            console.log('βœ… Bot berhasil keluar.');
        }
    }, {
        timezone: "Asia/Jakarta"
    });
});

// ========================================================
// FITUR OTOMATIS MULTI-SERVER (RESPON SLASH COMMAND)
// ========================================================
client.on('interactionCreate', async interaction => {
    if (!interaction.isChatInputCommand()) return;

    const { commandName, guildId, member } = interaction;

    if (commandName === 'join') {
        // Otomatis deteksi di Voice Channel mana USER yang mengetik perintah berada
        const voiceChannel = member.voice.channel;
        
        if (!voiceChannel) {
            return await interaction.reply('❌ Kamu harus masuk ke salah satu voice room dulu baru panggil aku!');
        }

        try {
            joinVoiceChannel({ 
                channelId: voiceChannel.id, 
                guildId: guildId, 
                adapterCreator: voiceChannel.guild.voiceAdapterCreator, 
                selfDeaf: true 
            });
            await interaction.reply(`πŸ€– Siap! Bot sudah masuk ke room **${voiceChannel.name}**.`);
        } catch (error) {
            console.error(error);
            await interaction.reply('❌ Gagal masuk, pastikan aku punya izin (permission) di room itu.');
        }
    }

    if (commandName === 'leave') {
        const connection = getVoiceConnection(guildId);
        if (connection) {
            connection.destroy();
            await interaction.reply('πŸ€– Bot berhasil keluar dari room server ini.');
        } else {
            await interaction.reply('ℹ️ Bot sedang tidak ada di voice room server ini.');
        }
    }
});

client.login(TOKEN);

// ========================================================
// SERVER WEB SEBAGAI SYARAT HOSTING DI RENDER.COM
// ========================================================
const app = express();
const PORT = process.env.PORT || 3000;
app.get('/', (req, res) => { res.send('πŸ€– Bot Jaga Room Aktif 24/7!'); });
app.listen(PORT, () => { console.log(`🌐 Server web berjalan di port ${PORT}`); });