Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs');
- const path = require('path');
- const gTTS = require('gtts');
- const { Client, GatewayIntentBits } = require('discord.js');
- const {
- joinVoiceChannel,
- createAudioPlayer,
- createAudioResource,
- AudioPlayerStatus,
- StreamType
- } = require('@discordjs/voice');
- const { TOKEN, GUILD_ID, VOICE_CHANNEL_ID } = require('./config');
- const client = new Client({
- intents: [GatewayIntentBits.GuildVoiceStates]
- });
- client.once('ready', async () => {
- console.log(`Logged in as ${client.user.tag}`);
- try {
- const guild = await client.guilds.fetch(GUILD_ID);
- const channel = await guild.channels.fetch(VOICE_CHANNEL_ID);
- if (!channel || channel.type !== 2) {
- console.log('❌ Это не голосовой канал или ID неверный');
- return;
- }
- const connection = joinVoiceChannel({
- channelId: channel.id,
- guildId: guild.id,
- adapterCreator: guild.voiceAdapterCreator,
- selfDeaf: false,
- });
- const player = createAudioPlayer();
- connection.subscribe(player);
- console.log('✅ Бот подключен и слушает lines.txt');
- // Мониторинг файла
- setInterval(() => {
- let lines = fs.readFileSync(path.join(__dirname, 'lines.txt'), 'utf-8')
- .split('\n')
- .filter(Boolean);
- if (lines.length == 0) { return }
- const line = lines.shift(); // берём первую строку
- fs.writeFileSync(path.join(__dirname, 'lines.txt'), lines.join('\n')); // удаляем её из файла
- const audioPath = path.join(__dirname, 'output.mp3');
- const tts = new gTTS(line, 'ru');
- tts.save(audioPath, () => {
- const resource = createAudioResource(fs.createReadStream(audioPath), {
- inputType: StreamType.Arbitrary
- });
- player.play(resource);
- });
- }, 100);
- } catch (e) {
- console.error('Ошибка подключения:', e);
- }
- });
- client.login(TOKEN);
Advertisement
Add Comment
Please, Sign In to add comment