Spaces:
Runtime error
Runtime error
Download index.js from vieryando/bot-discord-timer: direct link, hf CLI and curl.
- Browser
- Download file 5.14 kB
-
https://huggingface.co/spaces/vieryando/bot-discord-timer/resolve/main/index.js
- Command line
-
hf download hf://spaces/vieryando/bot-discord-timer/index.js
-
curl -L -o index.js https://huggingface.co/spaces/vieryando/bot-discord-timer/resolve/main/index.js
5.14 kB
| 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}`); }); |