Spaces:
Runtime error

bot-discord-timer / index.js
vieryando's picture
Upload 2 files
be95fdc verified
Raw History Blame Contribute Delete
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}`); });