使用discord.py构建一个discord机器人,我和Spotify API集成,打印播放列表中的歌曲列表
我现在的代码一次打印每首歌一句话(总共50句),这是非常低效的,意味着大量的ping。有没有办法把它全部打印成一大块或少于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']
)
最简单的方法可能是制作一个临时字符串,用于“构建”曲目列表:
制作一个保存所有数据的列表,然后使用
.join()
发送它我建议使用
f string
来简化字符串另一种方法是将数据保存到单独的变量中,以嵌入3列
相关问题 更多 >
编程相关推荐