首页 > 解决方案 > 我将如何检测用户活动?| 不和谐.py

问题描述

我正在尝试制作一个机器人,当输入命令时它会检测用户活动。我写了一些代码,但我得到的机器人的响应不是我想要的。那是我的代码:

from discord import Member
from discord.ext import commands

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


@bot.command()
async def status(ctx):
    await ctx.send(Member.activities)

bot.run('token')

这就是我得到的回应:

<“成员”对象的成员“活动”>

我怎样才能解决这个问题?有人会帮助我吗?

标签: pythonpython-3.xdiscord.py

解决方案


看来您是 python 新手。Python 是一种面向对象的编程语言,这意味着您需要区分类和实例。

在您的情况下,您正在获取类属性,尽管您需要实例属性。

你想做什么:

@bot.command
async def status(ctx):
  await ctx.send(ctx.author.activities)

不过,这会发送一个 python 格式的列表,所以这仍然不是你想要的。

我猜你想做什么:

@bot.command
async def status(ctx):
  await ctx.send(ctx.author.activities[0].name)

请注意,您需要更多代码,因为如果成员没有任何活动,这样的命令会引发错误。


推荐阅读