同时在discord.py中运行两个命令

2024-04-27 16:23:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个从youtube视频下载音频并上传到Anonfile的小机器人。以下是该点的代码:

@commands.command(name='audio')
async def audio_download(self, ctx, name, *, link: str):
    cwd = os.getcwd()
    path = f'{cwd}/media/audio'
    
    ydl_opts = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
        'outtmpl': f'{path}/{name}.mp3',
        'quiet': True
    }

    abspath = f'{path}/{name}.mp3'

    with ytb.YoutubeDL(ydl_opts) as ydl:
        ydl.download([link])

    r = requests.post('https://api.anonymousfiles.io',
                    files={'file': open(f'{abspath}', 'rb')})

    json_all = json.loads(r.text)
    url_file = json_all["url"]

    await ctx.send(f'File: {url_file}')

    os.remove(abspath)

问题是当两个不同的人想同时下载。当这段代码运行时,bot停止响应任何命令,直到任务结束,我的意思是,直到文件上传到anonfile。有没有一种方法可以让这段代码互不关联,让两个人可以同时使用它?人们A想从视频X下载音频,而人们B想从视频Y下载音频


Tags: path代码namejsonurl视频downloadlink