为什么我不断收到意外缩进错误? (不和谐.py)

时间:2021-03-18 19:28:53

标签: python discord.py

向我的音乐机器人添加播放命令后,我的音乐机器人出现意外缩进错误。有没有人明白为什么会这样?我无法通过 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')

2 个答案:

答案 0 :(得分:1)

您错过了尝试的结尾:

 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')
            

答案 1 :(得分:0)

您可能在某处使用了制表符,而在其他地方使用了实际空格。你有没有通过像 pylint、cake 或 mccabe 这样的 linter 运行它?