Vibe coding begins
This commit is contained in:
106
voice_bot.py
Normal file
106
voice_bot.py
Normal file
@@ -0,0 +1,106 @@
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
import speech_recognition as sr
|
||||
import asyncio
|
||||
import os
|
||||
|
||||
# Konfiguracja intencji (uprawnień)
|
||||
intents = discord.Intents.default()
|
||||
intents.message_content = True
|
||||
intents.voice_states = True
|
||||
|
||||
bot = commands.Bot(command_prefix="!", intents=intents)
|
||||
r = sr.Recognizer()
|
||||
|
||||
# Funkcja callback - uruchamiana po zakończeniu nagrywania
|
||||
async def finished_callback(sink, channel: discord.TextChannel, *args):
|
||||
print("Nagrywanie zakończone. Przetwarzanie audio...")
|
||||
|
||||
for user_id, audio in sink.audio_data.items():
|
||||
# Zapisz audio użytkownika do pliku tymczasowego
|
||||
filename = f"audio_{user_id}.wav"
|
||||
with open(filename, "wb") as f:
|
||||
# audio.file is a BytesIO-like object; use getvalue() to get bytes
|
||||
f.write(audio.file.getvalue())
|
||||
|
||||
# Próba rozpoznania mowy
|
||||
try:
|
||||
with sr.AudioFile(filename) as source:
|
||||
# Załaduj audio do pamięci
|
||||
audio_data = r.record(source)
|
||||
# Użyj Google Web Speech API (darmowe, ale ma limity)
|
||||
text = r.recognize_google(audio_data, language="pl-PL")
|
||||
|
||||
user = await bot.fetch_user(user_id)
|
||||
output = f"Użytkownik {user.name} powiedział: {text}"
|
||||
|
||||
# Print do konsoli (zgodnie z prośbą)
|
||||
print(output)
|
||||
# Opcjonalnie wyślij na kanał tekstowy:
|
||||
# await channel.send(output)
|
||||
|
||||
except sr.UnknownValueError:
|
||||
print(f"Nie zrozumiano mowy użytkownika {user_id}.")
|
||||
except sr.RequestError as e:
|
||||
print(f"Błąd serwisu Google Speech Recognition: {e}")
|
||||
except Exception as e:
|
||||
print(f"Wystąpił błąd: {e}")
|
||||
finally:
|
||||
# Posprzątaj pliki tymczasowe
|
||||
if os.path.exists(filename):
|
||||
os.remove(filename)
|
||||
|
||||
@bot.event
|
||||
async def on_ready():
|
||||
print(f'Zalogowano jako {bot.user}')
|
||||
|
||||
@bot.command()
|
||||
async def join(ctx):
|
||||
"""Bot dołącza do Twojego kanału głosowego"""
|
||||
if ctx.author.voice:
|
||||
channel = ctx.author.voice.channel
|
||||
await channel.connect()
|
||||
await ctx.send(f"Dołączono do {channel}")
|
||||
else:
|
||||
await ctx.send("Musisz być na kanale głosowym!")
|
||||
|
||||
@bot.command()
|
||||
async def start(ctx):
|
||||
"""Zacznij nagrywanie"""
|
||||
if ctx.voice_client:
|
||||
print("Rozpoczynam nagrywanie...")
|
||||
# WaveSink nagrywa do formatu WAV
|
||||
ctx.voice_client.start_recording(
|
||||
discord.sinks.WaveSink(),
|
||||
finished_callback,
|
||||
ctx.channel,
|
||||
)
|
||||
await ctx.send("Nasłuchuję... wpisz !stop aby transkrybować.")
|
||||
else:
|
||||
await ctx.send("Bot nie jest połączony z kanałem głosowym.")
|
||||
|
||||
@bot.command()
|
||||
async def stop(ctx):
|
||||
"""Zatrzymaj nagrywanie i wypisz tekst"""
|
||||
if ctx.voice_client:
|
||||
print("Zatrzymywanie nagrywania...")
|
||||
ctx.voice_client.stop_recording()
|
||||
await ctx.send("Zatrzymano nagrywanie. Sprawdź konsolę.")
|
||||
else:
|
||||
await ctx.send("Bot nic nie nagrywa.")
|
||||
|
||||
@bot.command()
|
||||
async def leave(ctx):
|
||||
"""Bot wychodzi z kanału"""
|
||||
if ctx.voice_client:
|
||||
await ctx.voice_client.disconnect()
|
||||
await ctx.send("Rozłączono.")
|
||||
else:
|
||||
await ctx.send("Bot nie jest połączony.")
|
||||
|
||||
# Uruchom bota
|
||||
token = os.getenv("DISCORD_TOKEN")
|
||||
if not token:
|
||||
raise SystemExit("Missing DISCORD_TOKEN environment variable. Set it and retry.")
|
||||
|
||||
bot.run(token)
|
||||
Reference in New Issue
Block a user