【问题标题】:Discord.py (Rewrite) on_member_update not working properlyDiscord.py(重写)on_member_update 无法正常工作
【发布时间】:2021-02-26 12:39:25
【问题描述】:

我对 discord.py 包的 on_member_update 函数有疑问。 我的问题是 on_member_update 函数仅在机器人本身更新(如角色更新)时触发,而不是在其他用户更新时触发(它应该如何工作)。 这是我的代码的细分:

import discord
from discord.ext import commands
import sys


TOKEN = 'myToken'
BOT_PREFIX = '!'



Intents = discord.Intents()
Intents.members = True
Intents.presences = True

bot = commands.Bot(command_prefix=BOT_PREFIX, Intents = Intents)

@bot.event
async def on_ready():
    print("Logged in as: " + bot.user.name + "\n")

@bot.event
async def on_member_update(before, after):
    if str(before.status) == "online":
        if str(after.status) == "offline":
            print("{} has gone {}.".format(after.name,after.status))
    else:
        print("lol")

@bot.command(pass_context=True)
async def stop(ctx):
    await ctx.author.send("bot stopped")
    sys.exit(0)

bot.run(TOKEN)

希望您知道如何让它发挥作用。 感谢您的帮助!

【问题讨论】:

  • 您可能没有启用 Intent。如果是这种情况,您可以这样做enable them :)

标签: python discord discord.py


【解决方案1】:

您应该从开发人员门户中为您的机器人启用存在意图,如下图所示。

discord developer portal

https://i.stack.imgur.com/cAn8m.png

【讨论】:

  • 它已经在我这边激活了 - 从 discord.py 迁移到 nextcord 后 on_member_update 不会触发
【解决方案2】:

接受的解决方案对我不起作用。

我需要改变:

on_member_update 到 on_presence_update 根据迁移指南:

https://nextcord.readthedocs.io/en/latest/migrating_2.html

"on_presence_update() 替换 on_member_update() 以更新 Member.status 和 Member.activities。 "

【讨论】:

    猜你喜欢
    • 2020-09-25
    • 2023-03-30
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-23
    • 1970-01-01
    • 2012-11-13
    相关资源
    最近更新 更多