Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Discord.py生成队列命令_Python_Bots_Discord.py - Fatal编程技术网

Python Discord.py生成队列命令

Python Discord.py生成队列命令,python,bots,discord.py,Python,Bots,Discord.py,我正在制作一个discord.py音乐机器人(我的第一个),我想知道如何排队。我猜它与asyncio命令(importasyncio)有关,但我真的不知道 from discord.ext import commands from discord.utils import get import asyncio import youtube_dl import os bot = commands.Bot(command_prefix='>') bot.remove_command('hel

我正在制作一个discord.py音乐机器人(我的第一个),我想知道如何排队。我猜它与
asyncio
命令(
importasyncio
)有关,但我真的不知道

from discord.ext import commands
from discord.utils import get
import asyncio
import youtube_dl
import os

bot = commands.Bot(command_prefix='>')
bot.remove_command('help')

@bot.event
async def on_ready():
    await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name=">help"))
    print("Bot is online! Logged in as: " + bot.user.name + "\n")

@bot.command(pass_context=True)
async def ping(ctx):
    await ctx.send(f'**Pong!** Latency: {round(bot.latency * 1000)}ms')

@bot.command(pass_context=True, aliases=['j'])
async def join(ctx):
    channel = ctx.message.author.voice.channel
    voice = get(bot.voice_clients, guild=ctx.guild)

    if voice and voice.is_connected():
        await voice.move_to(channel)
    else:
        voice = await channel.connect()

    await voice.disconnect()

    if voice and voice.is_connected():
        await voice.move_to(channel)
    else:
        voice = await channel.connect()


@bot.command(pass_context=True, aliases=['l'])
async def leave(ctx):
    channel = ctx.message.author.voice.channel
    voice = get(bot.voice_clients, guild=ctx.guild)

    if voice and voice.is_connected():
        await voice.disconnect()
    else:
        print("Bot was told to leave voice channel, but was not in one.")
        await ctx.send("OneBeat is not connected to a voice channel. Connect OneBeat to a voice channel before using this command.")


@bot.command(pass_context=True, aliases=['p'])
async def play(ctx, url: str):
    channel = ctx.message.author.voice.channel
    voice = get(bot.voice_clients, guild=ctx.guild)

    if voice and voice.is_connected():
        await voice.move_to(channel)
    else:
        voice = await channel.connect()

    await voice.disconnect()

    if voice and voice.is_connected():
        await voice.move_to(channel)
    else:
        voice = await channel.connect()

    song_there = os.path.isfile("song.mp3")
    try:
        if song_there:
            os.remove("song.mp3")
            print("Removed old song file.")
    except PermissionError:
        print("Trying to delete song file, but it's being played")
        await ctx.send("Error: Music is already playing (Queue feature coming soon).")
        return
    await ctx.send("One second...")

    voice = get(bot.voice_clients, guild=ctx.guild)

    ydl_opts = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
    }

    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        print("Downloading audio now\n")
        ydl.download([url])

    for file in os.listdir("./"):
        if file.endswith(".mp3"):
            name = file
            print(f"Renamed File: {file}\n")
            os.rename(file, "song.mp3")

    voice.play(discord.FFmpegPCMAudio("song.mp3"), after=lambda e: print("Song done!"))
    voice.source = discord.PCMVolumeTransformer(voice.source)
    voice.source.volume = 0.5

    nname = name.rsplit("-", 2)
    await ctx.send(f"Now playing: {nname[0]}")
    print("Playing\n")

bot.run('token')

不是真的!我不知道如何使用asyncio,我仍然设法创建队列。我的方法是创建一个包含字典和列表的json文件。看起来是这样的:

{
“queueskey”:[
{
“channelid”:[],
“队列”:[],
“地位”:[]
},
{
“channelid”:[],
“队列”:[],
“地位”:[]
},
{
“channelid”:[],
“队列”:[],
“地位”:[]
}
]
}
类似的事情一遍又一遍地重复着。你也可以存储额外的信息。尽管我花了相当长的时间进行调试并使其正常工作,但我认为这是最简单的方法之一。

看看