【问题标题】:Why do I keep getting an Unexpected Indent error? (discord.py) [duplicate]为什么我不断收到 Unexpected Indent 错误? (discord.py)[重复]
【发布时间】:2021-03-19 03:32:12
【问题描述】:

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

【问题讨论】:

  • 错误发生在哪一行?
  • ping 命令在@client.command() 上发生

标签: python discord.py


【解决方案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')
            

【讨论】:

    【解决方案2】:

    您可能在某处使用过制表符,而在其他地方使用过实际空格。您是否通过 pylint、cake 或 mccabe 之类的 linter 运行过它?

    【讨论】:

    • 混合使用空格和制表符会产生不同的错误。
    猜你喜欢
    • 2021-12-10
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 2020-12-28
    • 2014-04-12
    • 1970-01-01
    相关资源
    最近更新 更多