首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在discord.spotify中使用discord.py类

如何在discord.spotify中使用discord.py类
EN

Stack Overflow用户
提问于 2022-04-04 20:18:51
回答 1查看 151关注 0票数 0

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

代码语言:javascript
复制
@client.event
async def on_ready():
  print("Bot was connected to the server")
  
  await bot.change_presence(activity=discord.Spotify(title = "Test"))

我的输出是错的。

代码语言:javascript
复制
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'

完整源代码:

代码语言:javascript
复制
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类?

EN

回答 1

Stack Overflow用户

发布于 2022-04-04 20:41:00

要将机器人的状态更改为"listening to Spotify",可以这样做:

代码语言:javascript
复制
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name="Spotify"))

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

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71743303

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档