디스코드로 VC 오디오를 녹음하세요!

7632 단어 discordpython
나는 discord.py의 도움으로 vc의 오디오를 녹음할 수 있는 이 사이드 프로젝트를 진행하고 있습니다.

이 게시물에서 내가 이것을 달성한 방법에 대해 이야기합니다. 당신은 또한 이것에 대해 내가 만든 것을 확인할 수 있습니다.

재미있는 부분에 도달하기 전에 discord developer portal을 통해 봇을 만들고 설정해야 합니다. 또한 discord.py와 작동 방식에 대한 상당히 많은 지식이 필요합니다!

이제 기본 테스트 명령이 작동하는 봇이 있으면 필요한 모듈을 가져올 수 있습니다!

import discord
from discord.ext import commands
from discord.ext.audiorec import NativeVoiceClient #important!
from secrets import token #bot's secret token
import random

이제 vc에 오디오를 녹음하려면 먼저 봇이 사용자가 있는 vc에 조인하도록 해야 합니다. 이 작업은 다음 명령을 사용하여 수행할 수 있습니다.

@client.command()
async def join(ctx: commands.Context):
    channel: discord.VoiceChannel = ctx.author.voice.channel
    if ctx.voice_client is not None:
        return await ctx.voice_client.move_to(channel)
    await channel.connect(cls=NativeVoiceClient)
    await ctx.invoke(client.get_command('rec'))

보시다시피 이 명령은 'rec'라는 명령을 호출합니다. 이 'rec' 명령은 vc에 오디오를 녹음하는 데 사용할 기본 명령입니다!

@client.command()
async def rec(ctx):
    ctx.voice_client.record(lambda e: print(f"Exception: {e}"))
    embedVar = discord.Embed(title="Started the Recording!",
                             description="use !stop to stop!", color=0x546e7a)
    await ctx.send(embed=embedVar)

녹음을 시작한 다음 녹음이 시작되었음을 사용자에게 알리는 임베딩을 보냅니다!
이 명령을 사용하여 녹음을 중지하고 저장할 수 있습니다.

@client.command()
async def stop(ctx: commands.Context):
    if not ctx.voice_client.is_recording():
        return
    await ctx.send(f'Stopping the Recording')

    wav_bytes = await ctx.voice_client.stop_record()

    name = str(random.randint(000000, 999999))
    with open(f'{name}.wav', 'wb') as f:
        f.write(wav_bytes)
    await ctx.voice_client.disconnect()

여기에서 봇은 기본적으로 모든 오디오 데이터를 바이트 형태로 가져온 다음 모든 데이터를 wav 파일(이 파일의 이름은 임의로 생성됨)에 쓰고 시스템에 저장합니다.
이제 봇의 작업이 완료되었으므로 vc를 떠납니다!

몇 가지 추가 사항:
봇이 vc에 합류하기 전에 vc에 누군가가 있는지 확인하거나 오디오 파일을 클라우드의 서버에 업로드하는 명령을 추가할 수도 있습니다. 이 모든 사항은 내 에서 논의됩니다. 자유롭게 확인하십시오!

이 프로젝트의 모든 코드는 github에서도 사용할 수 있습니다!


realhardik18 / discord.py-음성 녹음기


discord.py의 오디오 채널에 vc를 녹음하기 위한 저장소

좋은 웹페이지 즐겨찾기