微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何让我的不和谐机器人一次打印整个播放列表?

如何解决如何让我的不和谐机器人一次打印整个播放列表?

使用 discord.py 构建一个不和谐机器人,我集成了 Spotify API 以打印播放列表中的歌曲列表。

我现在的代码每次打印每首歌曲 1 个句子(总共 50 个),这非常低效并且意味着很多 ping。关于如何将其全部打印成 1 个大块或小于 50 个的任何想法?

@client.command(name="ukmusic")
async def playlist(ctx):
    #Authortization for API usage
    headers = {
        "Authorization": "Bearer {}".format(oauth)
        }
    #These create the link that the program fetches (The playlist)
    endpoint = "https://api.spotify.com/v1/playlists/37i9dQZEVXbLnolsZ8PSNw"
    data = urlencode({"market": "GB"})
    lookup_url = f"{endpoint}?{data}"
    #This prints what the link looks like and the status code (200 if it works correctly)
    print (lookup_url)
    r = requests.get(lookup_url,headers = headers)
    print (r.status_code)

    #This prints out the playlist 
    await ctx.send("Here is what the good people of Britiania are listening to on Spotify")
    em = discord.Embed(title = "Song - Artist - Album\n")

    for item in r.json()['tracks']['items']:
        await ctx.send(
            item['track']['name'] + ' - ' +
            item['track']['artists'][0]['name'] + ' - ' +
            item['track']['album']['name']
        )

解决方法

最简单的方法可能是制作一个临时字符串,用于“构建”您的曲目列表:


@client.command(name="ukmusic")
async def playlist(ctx):
    #Authortization for API usage
    headers = {
        "Authorization": "Bearer {}".format(oauth)
        }
    #These create the link that the program fetches (The playlist)
    endpoint = "https://api.spotify.com/v1/playlists/37i9dQZEVXbLnolsZ8PSNw"
    data = urlencode({"market": "GB"})
    lookup_url = f"{endpoint}?{data}"
    #This prints what the link looks like and the status code (200 if it works correctly)
    print (lookup_url)
    r = requests.get(lookup_url,headers = headers)
    print (r.status_code)

    #This prints out the playlist 
    await ctx.send("Here is what the good people of Britiania are listening to on Spotify")
    em = discord.Embed(title = "Song - Artist - Album\n")

    allTracks = ""

    for item in r.json()['tracks']['items']:
        allTracks += item['track']['name'] + ' - ' + item['track']['artists'][0]['name'] + ' - ' + item['track']['album']['name'] + '\n'

    await ctx.send(allTracks)

,

制作一个保存所有数据的列表,然后使用 .join() 发送。

我建议使用 f string 以更简单的方式制作字符串。

然后你所说的另一种方法是将数据保存到单独的变量中以嵌入 3 列

@client.command(name="ukmusic")
async def playlist(ctx):
    #Authortization for API usage
    headers = {
        "Authorization": "Bearer {}".format(oauth)
        }
    #These create the link that the program fetches (The playlist)
    endpoint = "https://api.spotify.com/v1/playlists/37i9dQZEVXbLnolsZ8PSNw"
    data = urlencode({"market": "GB"})
    lookup_url = f"{endpoint}?{data}"
    #This prints what the link looks like and the status code (200 if it works correctly)
    print (lookup_url)
    r = requests.get(lookup_url,headers = headers)
    print (r.status_code)

    #This prints out the playlist 
    em = discord.Embed(title = "Here is what the good people of Britiania are listening to on Spotify")

    tracks_names = []
    tracks_artists = []
    tracks_album = []

    # save the data into lists (could be better using a dict with a nested list
    for item in r.json()['tracks']['items']:
        tracks_names.append(item['track']['name'])
        tracks_artists.append(item['track']['artists'][0]['name'])
        tracks_album.append(item['track']['album']['name'])
        
    # add the 3 columns 
    em.add_field(name="Song",value='\n'.join(tracks_names),inline=True)
    em.add_field(name="Artist",value='\n'.join(tracks_artists),inline=True)
    em.add_field(name="Album",value='\n'.join(tracks_album),inline=True)

    await ctx.send(embed=em)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。