Код: Выделить всё
import sys
import discord
from discord.ext import commands
import vosk
import asyncio
import json
import pyaudio
import threading
from discord import FFmpegPCMAudio, PCMVolumeTransformer
TOKEN = 'token' #changeit
MODEL_PATH = 'c:\\model'
DESTINATION_CHANNEL_ID = channelid #changeit
intents = discord.Intents.default()
intents.messages = True
intents.voice_states = True
bot = commands.Bot(command_prefix='!', intents=intents)
model = vosk.Model(MODEL_PATH)
audio_queue = asyncio.Queue()
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16,
channels=1,
rate=16000,
input=True,
frames_per_buffer=8000)
def read_audio():
while True:
data = stream.read(8000)
audio_queue.put_nowait(data)
audio_thread = threading.Thread(target=read_audio)
audio_thread.start()
async def process_audio(vc):
rec = vosk.KaldiRecognizer(model, 16000)
while vc.is_connected():
data = await audio_queue.get()
if rec.AcceptWaveform(data):
result = json.loads(rec.Result())
print(f"Detected speech: {result['text']}")
if 'keyword' in result['text']:
destination_channel = bot.get_channel(DESTINATION_CHANNEL_ID)
for member in vc.channel.members:
if member != bot.user:
await member.move_to(destination_channel, reason='Moved for saying restricted phrase')
@bot.event
async def on_ready():
print(f'Logged in as {bot.user}')
@bot.event
async def on_voice_state_update(member, before, after):
if after.channel is not None and bot.user not in after.channel.members:
vc = await after.channel.connect()
asyncio.create_task(process_audio(vc))
bot.run(TOKEN)
Подробнее здесь: https://stackoverflow.com/questions/793 ... king-prope
Мобильная версия