【问题标题】:a task that check's a user's activity检查用户活动的任务
【发布时间】:2021-08-03 10:35:35
【问题描述】:

我想创造一种方法,让机器人在用户输入消息时检查用户的状态,并在用户状态包含特定字词时赋予他们角色。这是我的代码 atm,是的,我已启用特权意图,并且我正在 on_message 事件中启动循环命令。问题是它返回“ctx 是一个缺少的位置参数:/ 现在我该如何解决这个问题?

@tasks.loop()
async def check(ctx):
  supporter = discord.utils.get(ctx.guild.roles, name="Supporter")
  member = ctx.author
  if ".gg/eternalgw" in member.activity:
    await ctx.send(f"I gave {member.mention} the Supporter role for supporting us!")
    await member.add_role(supporter)```

【问题讨论】:

  • 如何在 tasks.loop 中获得上下文?遍历所有存在的成员,或触发任何消息
  • 好的,我尝试在没有 tasks.loop 的 on_message 语句中触发它,但它不起作用,我没有使用 ctx。

标签: python discord discord.py


【解决方案1】:

tasks.loop 不要将ctx 作为参数,因为它没有任何意义。如果你真的想用循环检查状态,你必须检查公会的所有成员。但是,如果您只想在有人发送消息时执行此代码,请使用 on_message 事件:

@bot.event
async def on_message(message):
  supporter = discord.utils.get(message.guild.roles, name="Supporter")
  member = message.author
  if ".gg/eternalgw" in str(member.activity):
    await message.channel.send(f"I gave {member.mention} the Supporter role for supporting us!")
    await member.add_roles(supporter)

【讨论】:

  • 这不起作用,我在做任务之前尝试过这样做,但它不起作用。
  • 忽略 on_message Traceback 中的异常(最近一次调用最后一次):文件“/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py”,第 343 行,在_run_event await coro(*args, **kwargs) File "main.py", line 189, in on_message if "Dm" in member.activity: TypeError: argument of type 'CustomActivity' is not iterable
  • 好吧,所以我将它转换为字符串,现在可以使用了!
  • 现在的问题是它没有添加角色:/
  • 忽略 on_message Traceback 中的异常(最近一次调用最后一次):文件“/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py”,第 343 行,在_run_event await coro(*args, **kwargs) File "main.py", line 192, in on_message await member.add_role(supporter) AttributeError: 'Member' object has no attribute 'add_role'
猜你喜欢
  • 1970-01-01
  • 2011-01-27
  • 1970-01-01
  • 2013-07-25
  • 2013-05-20
  • 1970-01-01
  • 1970-01-01
  • 2012-02-11
  • 1970-01-01
相关资源
最近更新 更多