为什么我总是遇到意外的缩进错误?(discord.py)

2024-04-28 17:12:16 发布

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

向我的音乐机器人添加播放命令后,我的音乐机器人出现意外的缩进错误。有人知道为什么会这样吗?我无法通过ping命令找到任何意外的缩进,但它仍然给出了错误。感谢所有的帮助

import discord
import youtube_dl
import subprocess
import os
from discord import FFmpegPCMAudio
from discord.ext import commands

client = commands.Bot(command_prefix = '.')

@client.event
async def on_ready():
    print("bot online")

@client.command()
async def play(ctx, url):
    song_there = os.path.isfile("song.mp3")
    try:
        if song_there:
            os.remove("song.mp3")
    except PermissionError:
        await ctx.send("There is already music playing!")

    ydl_opts = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
    }
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([url])
    for file in os.listdir("./"):
        if file.endswith(".mp3"):
            os.rename(file, "song.mp3")
    voice.play(discord.FFmpegPCMAudio("song.mp3"))

    try:
        channel = ctx.message.author.voice.channel
    except:
        await ctx.send(":x: Please get in a voice channel, then try again!")
        return
    
    try:
        global voice
        voice = await channel.connect()
        await voice.connect()

@client.command()
async def ping(ctx):
    await ctx.send(f"Pong! {round(client.latency * 1000)}")

client.run('TOKEN')

Tags: importclientasyncsongosdefchannelawait
2条回答

您错过了尝试的终点:

 try:
    global voice
    voice = await channel.connect()
    await voice.connect()
 except:
    print("An exception occurred")

@client.command()
async def ping(ctx):
    await ctx.send(f"Pong! {round(client.latency * 1000)}")

client.run('TOKEN')
            

您可能在其他地方使用了制表符,而在其他地方使用了实际空格。你有没有用皮林、蛋糕或麦卡比这样的皮棉做过

相关问题 更多 >