119 lines
3.3 KiB
JavaScript
119 lines
3.3 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
process.env.FFMPEG_PATH = require('ffmpeg-static');
|
|
|
|
const {
|
|
Client,
|
|
GatewayIntentBits
|
|
} = require('discord.js');
|
|
const {
|
|
joinVoiceChannel,
|
|
createAudioPlayer,
|
|
createAudioResource,
|
|
AudioPlayerStatus,
|
|
VoiceConnectionStatus,
|
|
getVoiceConnection
|
|
} = require('@discordjs/voice');
|
|
|
|
// --- CONFIGURATION ---
|
|
let TOKEN, CLIENT_ID;
|
|
try {
|
|
const config = require('./config.json');
|
|
TOKEN = config.DISCORD_TOKEN;
|
|
CLIENT_ID = config.DISCORD_CLIENT_ID;
|
|
} catch (err) {
|
|
console.warn('Warning: config.json not found. Using environment variables if available.');
|
|
TOKEN = process.env.DISCORD_TOKEN;
|
|
CLIENT_ID = process.env.DISCORD_CLIENT_ID;
|
|
}
|
|
|
|
if (!TOKEN) {
|
|
console.error('ERROR: DISCORD_TOKEN is missing. Please set it in the dashboard.');
|
|
}
|
|
|
|
const client = new Client({
|
|
intents: [
|
|
GatewayIntentBits.Guilds,
|
|
GatewayIntentBits.GuildVoiceStates,
|
|
]
|
|
});
|
|
|
|
// Helper function to join voice
|
|
function connectToVoice(interaction) {
|
|
const member = interaction.member;
|
|
const voiceChannel = member.voice.channel;
|
|
|
|
if (!voiceChannel) {
|
|
throw new Error('Kamu harus berada di Voice Channel untuk menggunakan perintah ini!');
|
|
}
|
|
|
|
return joinVoiceChannel({
|
|
channelId: voiceChannel.id,
|
|
guildId: interaction.guildId,
|
|
adapterCreator: interaction.guild.voiceAdapterCreator,
|
|
});
|
|
}
|
|
|
|
client.on('ready', () => {
|
|
console.log(`Logged in as ${client.user.tag}!`);
|
|
console.log('Bot Status: Online');
|
|
});
|
|
|
|
client.on('interactionCreate', async interaction => {
|
|
if (!interaction.isChatInputCommand()) return;
|
|
|
|
const { commandName } = interaction;
|
|
|
|
if (commandName === 'join') {
|
|
await interaction.deferReply();
|
|
try {
|
|
connectToVoice(interaction);
|
|
await interaction.editReply('Berhasil masuk ke Voice Channel!');
|
|
} catch (error) {
|
|
await interaction.editReply({ content: error.message, ephemeral: true });
|
|
}
|
|
}
|
|
|
|
if (commandName === 'sahur') {
|
|
await interaction.deferReply();
|
|
|
|
try {
|
|
let connection = getVoiceConnection(interaction.guild.id);
|
|
|
|
if (!connection) {
|
|
connection = connectToVoice(interaction);
|
|
}
|
|
|
|
const player = createAudioPlayer();
|
|
const resource = createAudioResource(path.join(process.cwd(), 'sahur.mp3'));
|
|
|
|
connection.subscribe(player);
|
|
player.play(resource);
|
|
|
|
console.log('Bot Status: Playing');
|
|
await interaction.editReply('🔊 Sedang memutar alarm sahur!');
|
|
|
|
player.on(AudioPlayerStatus.Idle, () => {
|
|
console.log('Bot Status: Finished playing (Idle)');
|
|
});
|
|
|
|
player.on('error', e => {
|
|
console.error('ERROR_AUDIO:', e.message);
|
|
interaction.followUp({ content: 'Terjadi kesalahan saat memutar audio.', ephemeral: true });
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('ERROR:', error);
|
|
await interaction.editReply(error.message || 'Gagal memutar audio.');
|
|
}
|
|
}
|
|
});
|
|
|
|
if (TOKEN) {
|
|
client.login(TOKEN).catch(err => {
|
|
console.error('Login failed:', err.message);
|
|
});
|
|
} else {
|
|
console.log('Bot Status: Waiting for token setup...');
|
|
}
|