#Voice recording by mute and unmute (on_voice_state_update) does function.

1 messages · Page 1 of 1 (latest)

arctic crescent
#

I can't figure out why this won't do anything:

@bot.command(name="listen", description="Start Listening")
async def record(ctx):

    voice = ctx.author.voice
    if not voice :
        return await ctx.respond("You are not in a voice channel!", ephemeral=True)
    
    else:
        vc = await voice.channel.connect()
        connections.update({ctx.guild.id: vc})
    
    async def on_voice_state_update():
        self_mute = ctx.author.voice.self_mute
        if self_mute == False:
            await ctx.respond("Listening, partner...", delete_after=5, ephemeral=True)
            vc.start_recording(
            discord.sinks.WaveSink(),
            once_done,
        )
            
        if self_mute == True:
            if ctx.guild.id in connections:
                vc = connections[ctx.guild.id]
                vc.stop_recording()
                del connections[ctx.guild.id]
                await ctx.delete()

    await ctx.respond("I will start to listen.")```

The once_done is a separate event that works on it's own so I think that the problem is in the actual mute-unmute part. I used to do it with buttons and it worked well but was unpractical. It would be great if anyone could help me figure this out :) Cheers! ![blurple](https://cdn.discordapp.com/emojis/1080020651357777992.webp?size=128 "blurple")
#

The bot connects to the voice channel properly, and sends the I will start to listen message. But never actually does anything after that.

#

Did I perhaps forget something?

arctic crescent
tranquil vine
#

and events do not work like that...

#

you need an event listener like @bot.listen() or @bot.event

arctic crescent
arctic crescent
tranquil vine
#

see that

bleak elkBOT
#

Here's the wait for event example.

tranquil vine
#

if it is useful

arctic crescent
#

So I guess it's not limited

tranquil vine
#

alr

#

.close

brittle pivotBOT
#

Done with your help thread?

Please close your own help thread by using </close:1009144375709814897> with @bleak elk.

tranquil vine
#

if you are done

arctic crescent
arctic crescent
tranquil vine
#

like only wait for a bot event when a command is executed?

arctic crescent
#

I mean like, if the command hasn't been executed the event should not work.

arctic crescent
tranquil vine
#

ah

tranquil vine
#

if you really want then you can play around with bot vars, but that wouldn't a very elegant solution

tranquil vine
arctic crescent
#

So bot.wait_for(bot.command(listen)) or something around those lines?

tranquil vine
arctic crescent
tranquil vine
arctic crescent
#

Oh, okay

tranquil vine
#

yea then you can use bot.wait_for

#

pass "command" as the event to it

#

.rtfm bot.wait_for

arctic crescent
tranquil vine
tranquil vine
arctic crescent
#
@bot.event 
async def on_voice_state_update(ctx):
    self_mute = ctx.author.voice.self_mute
    if self_mute == False:
        await ctx.respond("Listening, partner...", delete_after=5, ephemeral=True)
        vc.start_recording(
        discord.sinks.WaveSink(),
        once_done,
    )
            
    if self_mute == True:
        if ctx.guild.id in connections:
            vc = connections[ctx.guild.id]
            vc.stop_recording()
            del connections[ctx.guild.id]
            await ctx.delete().
#
async def once_done(sink: discord.sinks, channel: discord.TextChannel, *args):  # Our voice client already passes these in.
    recorded_users = [  # A list of recorded users
        f"<@{user_id}>"
        for user_id, audio in sink.audio_data.items()]
    
    await sink.vc.disconnect()  # Disconnect from the voice channel.

    for user_id, audio in sink.audio_data.items():
        reply = await channel.send("Let me think about that partner...")
        with open(f'./Recordings/{user_id}.{sink.encoding}', 'wb+') as f:
            f.write(audio.file.read())
        with open(f"./prompts/chatGPT/western.txt", "r") as f:
            prompt = f.read()
        messages=[]
        file= open(f'./Recordings/{user_id}.{sink.encoding}', "rb")
        transcription = openai.Audio.transcribe("whisper-1", file)
        messages.append({"name":"System","role": "user", "content": prompt})
        print(transcription)
        messages.append({"role":"user", "content": transcription["text"]})
        response = await openai.ChatCompletion.acreate(
            model="gpt-3.5-turbo",
            messages=messages
        )
        response = response["choices"][0]["message"]["content"]
        print(response)
        f.close()
    await reply.edit(response)```
tranquil vine
#

.rtfm on_voice_state_update

tranquil vine
#

see what all it gives

arctic crescent
#

Thanks, I'll be right back

arctic crescent
#

The only one that is recognized is on_voice_state_update()

arctic crescent
tranquil vine
#

In the bot.event system

#

But there are other ways to override methods too

#

Not needed for this so don't worry about it

tranquil vine
#

If so, start recording

tranquil vine
arctic crescent
tranquil vine
arctic crescent
#
@bot.event
async def on_voice_state_update(ctx, member, before, after):
    self_mute = ctx.author.voice.self_mute
    if before.self_mute == False and after.self_mute == True:
        await ctx.respond("Listening, partner...", delete_after=5, ephemeral=True)
        vc.start_recording(
        discord.sinks.WaveSink(),
        once_done,
    )
            
    if before.self_mute == True and after.self_mute == False:
        if ctx.guild.id in connections:
            vc = connections[ctx.guild.id]
            vc.stop_recording()
            del connections[ctx.guild.id] 
            await ctx.delete()```
arctic crescent
#
@bot.event
async def on_voice_state_update(ctx, member, before, after):
    self_mute = ctx.author.voice.self_mute
    if before.self_mute == False and after.self_mute == True:
        await ctx.respond("Listening, partner...", delete_after=5, ephemeral=True)
        vc.start_recording(
        discord.sinks.WaveSink(),
        once_done,
    )
            
    if before.self_mute == True and after.self_mute == False:
        if ctx.guild.id in connections:
            vc = connections[ctx.guild.id]
            vc.stop_recording()
            del connections[ctx.guild.id] 
            await ctx.delete()
            
    if before.voice_state == after.voice_state:
        return
    
    if before.voice_state != None and after.voice_state == None:
        if ctx.guild.id in connections:
            vc = connections[ctx.guild.id]
            vc.stop_recording()
            del connections[ctx.guild.id] 
            await ctx.delete()```
tranquil vine
#

you will need to use bot vars to keep track of the channel to send the message in

#

store a mapping of vc channel id to text channel

#

oh wait i dont think you need botvars, because this is in main file

arctic crescent
#

Yeah

#

Automatically it sends the message in the channel the command was executed in.

#

Or I could just easily make a command to set a channel for it

#

But now how do I make it so it starts after the command has been executed?

arctic crescent
arctic crescent
#

I'm trying to check what I can replace ctx with but I can't think of anything cat_roaring

tranquil vine
#

create a dictionary, where key will be vc channel id, and value will be the channel

#

in the cmd where you start recording, store details in this dict ^

#

and in the event, get the channel from here

arctic crescent
tranquil vine
tranquil vine
#

it will only be present if the cmd has run

#

after you are done recording, remove the vc from that dict