简体   繁体   English

if 语句在歌词命令中不起作用

[英]if statement not working in lyrics command

So i recently made a lyrics command, it works but it returns the error: An error occurred: Command raised an exception: HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body In embed.description: Must be 2048 or fewer in length.所以我最近做了一个歌词命令,它可以工作,但它返回错误:发生错误:命令引发异常:HTTPException:400 错误请求(错误代码:50035):嵌入中的表单正文无效。描述:必须为 2048 或更少长度。 returns this error when the lyrics is more than 2048 characters.当歌词超过 2048 个字符时返回此错误。 i've already made a way for it to send a txt file instead of the error above but it isn't working.我已经为它发送了一个 txt 文件而不是上面的错误,但它不起作用。 Here's the code:这是代码:

@commands.command()
async def lyrics(self, ctx,*, title):
     url = f"https://some-random-api.ml/lyrics?title={title}"
     response = requests.get(url)
     json_data = json.loads(response.content)
     lyrics = json_data.get('lyrics')
     lyrics = json_data.get('lyrics')
     if lyrics: 
       em = discord.Embed(title=title,description=lyrics,color=0xa3a3ff)
       return await ctx.send(embed=em)
       if len(lyrics) > 2048:
         em = discord.Embed(title=title,description = f"I wasn't able to send the lyrics for that song since it exceeds 2000 characters. However, here's the file for the lyrics!",color=0xa3a3ff)
         await ctx.send(embed=em)
         file = open("lyrics.txt", "w")
         file.write(lyrics)
         file.close() 
         return await ctx.send(file=discord.File("lyrics.txt"))
     else:
        em = discord.Embed(title="Aw Snap!",description="I wasn't able to find the lyrics of that song.",color = 0xa3a3ff)
        em.set_thumbnail(url='https://cdn.discordapp.com/attachments/830818408550629407/839555682436251698/aw_snap_large.png')
        await ctx.send(embed=em)

Create the embed after you've checked the lyrics' length, ie if the length of the lyrics is > 2048 make the file and corresponding embed else make the normal embed.检查歌词长度后创建嵌入,即如果歌词长度> 2048,则制作文件和相应的嵌入,否则制作正常嵌入。 We can translate this naturally into code:我们可以自然地将其翻译成代码:

    # ...
    if lyrics:
        if len(lyrics) > 2048:
            # Lyrics are too large to send through embed
            em = discord.Embed(title=title,description = f"I wasn't able to send the lyrics for that song since it exceeds 2000 characters. However, here's the file for the lyrics!",color=0xa3a3ff)
            await ctx.send(embed=em)
            file = open("lyrics.txt", "w")
            file.write(lyrics)
            file.close() 
            return await ctx.send(file=discord.File("lyrics.txt"))
        else:
            # Lyrics can directly be sent through embed
            em = discord.Embed(title=title,description=lyrics,color=0xa3a3ff)
            return await ctx.send(embed=em)
    # ...

Your if statement is coming after sending message.您的 if 语句将在发送消息后出现。 Also you can send txt file without saving it.您也可以发送 txt 文件而不保存它。

import io

@commands.command()
async def lyrics(self, ctx,*, title):
    url = f"https://some-random-api.ml/lyrics?title={title}"
    response = requests.get(url)
    data = response.json()
    data = data.get('lyrics')
    title = data.get("title")
    author = data.get("author")
    lyrics = data.get("lyrics")
    if lyrics:
        if len(lyrics) < 2000:
            embed = discord.Embed(
                title = f"{title} - {author}",
                description = lyrics,
                color = 0xA3A3FF
            )
            await ctx.send(embed=embed)

        else:
            s = io.StringIO(lyrics)
            file = discord.File(s, f"{title} - {author}.txt")
            await ctx.send(file=file)

    else:
        await ctx.send("No lyrics could be found.")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM