繁体   English   中英

如何在discord.py中使用discord.spotify class

[英]How to use discord.spotify class in discord.py

discord.py 文档有一个 spotify class ( https://discordpy.readthedocs.io/en/stable/api.html#spotify ) 我想知道如何将此活动设置为配置文件。 文档中没有示例。 我尝试在 on_ready 事件中使用这个活动

@client.event
async def on_ready():
  print("Bot was connected to the server")
  
  await bot.change_presence(activity=discord.Spotify(title = "Test"))

而我的output是错的。

Bot was connected to the server

Ignoring exception in on_ready
Traceback (most recent call last):
  File "/home/runner/Russian-field-of-experiments-on-the-island-of-Python/venv/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 13, in on_ready
    await bot.change_presence(activity=discord.Spotify(title = "Test"))
  File "/home/runner/Russian-field-of-experiments-on-the-island-of-Python/venv/lib/python3.8/site-packages/discord/activity.py", line 527, in __init__
    self._sync_id = data.pop('sync_id')
KeyError: 'sync_id'

完整源代码:

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix="!")

@bot.event
async def on_ready():
  print("Bot was connected to the server")

 await bot.change_presence(activity=discord.Spotify(title = "Test"))

bot.run("token")

如何在 discord.py 中使用 Spotify class?

要将机器人的状态更改为"listening to Spotify" ,您可以执行以下操作:

await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name="Spotify"))

如果您想将其设置为特定用户正在收听的内容,您可以这样做:

from discord import Spotify

@bot.event
async def on_ready():
user = bot.get_user(user_id) # Make sure to change this to the users ID.
while True:
 for activity in user.activities:
        if isinstance(activity, Spotify):
           await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name=f"{activity.title} by {activity.artist}"))
        else:
             await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name=f"Nothing")) # User isn't listening to Spotify

要使上述工作正常,您定义的用户必须正在收听 Spotify。 如果他们不收听 Spotify,状态将更改为Nothing

暂无
暂无

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

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