歌词天才歌词有时以“EmbedShare URLCopyEmbedCopy”结尾

2024-09-26 22:55:33 发布

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

我正在制作一个不和谐的歌词机器人并接收歌词。我正在使用GeniusAPI(lyricsgeniusAPI包装器)。但当我收到歌词时,结尾是:

“away”是这首歌的最后一个词,但它伴随着EmbedShare URLCopyEmbedCopy。有时它只是简单的歌词,没有EmbedShare文本

用同一首歌:

有没有办法防止这种情况发生

lyrics命令的源代码:

@commands.command(help="Gives the lyrics of the song XD! format //lyrics (author) (song name)")
async def lyrics(self, ctx, arg1, arg2):
    song = genius.search_song(arg1, arg2)
    print(song.lyrics)
    name = ("Lyrics for " + arg2.capitalize() + " by " + arg1.capitalize())
    gembed = discord.Embed(title=name.capitalize(), description=song.lyrics)
    await ctx.send(embed=gembed)

Tags: thenamesong结尾机器人歌词ctxarg1
2条回答

Some Random API是一种很容易处理的方法,当您创建一个命令来发送歌词时

这是如何使用一些随机api实现的

# these imports are used for this particular lyrics command. the essential import here is aiohttp, which will be used to fetch the lyrics from the API
import textwrap
import urllib
import aiohttp
import datetime

@bot.command(aliases = ['l', 'lyrc', 'lyric']) # adding aliases to the command so they they can be triggered with other names
async def lyrics(ctx, *, search = None):
    """A command to find lyrics easily!"""
    if not search: # if user hasnt given an argument, throw a error and come out of the command
        embed = discord.Embed(
            title = "No search argument!",
            description = "You havent entered anything, so i couldnt find lyrics!"
        )
        return await ctx.reply(embed = embed)
        # ctx.reply is available only on discord.py version 1.6.0, if you have a version lower than that use ctx.send
    
    song = urllib.parse.quote(search) # url-encode the song provided so it can be passed on to the API
    
    async with aiohttp.ClientSession() as lyricsSession:
        async with lyricsSession.get(f'https://some-random-api.ml/lyrics?title={song}') as jsondata: # define jsondata and fetch from API
            if not 300 > jsondata.status >= 200: # if an unexpected HTTP status code is recieved from the website, throw an error and come out of the command
                return await ctx.send(f'Recieved poor status code of {jsondata.status}')

            lyricsData = await jsondata.json() # load the json data into its json form

    error = lyricsData.get('error')
    if error: # checking if there is an error recieved by the API, and if there is then throwing an error message and returning out of the command
        return await ctx.send(f'Recieved unexpected error: {error}')

    songLyrics = lyricsData['lyrics'] # the lyrics
    songArtist = lyricsData['author'] # the author's name
    songTitle = lyricsData['title'] # the song's title
    songThumbnail = lyricsData['thumbnail']['genius'] # the song's picture/thumbnail

    # sometimes the song's lyrics can be above 4096 characters, and if it is then we will not be able to send it in one single message on Discord due to the character limit
    # this is why we split the song into chunks of 4096 characters and send each part individually
    for chunk in textwrap.wrap(songLyrics, 4096, replace_whitespace = False):
        embed = discord.Embed(
            title = songTitle,
            description = chunk,
            color = discord.Color.blurple(),
            timestamp = datetime.datetime.utcnow()
        )
        embed.set_thumbnail(url = songThumbnail)
        await ctx.send(embed = embed)

这是一个已知的lyricsgenius错误,有一个开放的PR来解决这个问题:https://github.com/johnwmillr/LyricsGenius/pull/215

这是因为lyricsgeniusweb从Genius的网站上删除歌词,这意味着如果他们的网站更新,lyricsgenius将无法获取歌词。此库已6个月未更新;它本身就是一个web废弃库,这意味着这种不活动将使库严重不稳定。由于该库是根据MIT授权的,因此您可以分叉该库并为您的项目/机器人维护最新版本。不过,最好使用专用API来获取歌曲歌词,以保证稳定性

另外,lyricsgenius使用同步requests库,这意味着它将在获取歌词时“阻止”异步bot。这对于Discord机器人来说绝对是不可取的,因为你的机器人在获取歌词时会完全没有响应。考虑在调用阻塞函数时使用^ {CD6}}重写它或使用^{}

相关问题 更多 >

    热门问题