Yarman53
Yarman53

Reputation: 51

discord.py Member.activity returns custom status and not game

I am creating a bot that detects if user starts playing a game and gives an according role to the server member.

@client.event
async def on_member_update(before, after):
  role = discord.utils.get(after.guild.roles, name="roleName")
  games = ["Escape from Tarkov"]

  if after.activity and after.activity.name in games:
    await after.add_roles(role)

      #this takes back the role when the member exits the game
  elif before.activity and before.activity.name in games and not after.activity:
    if role in after.roles: # check they already have the role, as to not throw an error
      await after.remove_roles(role)

However, when a member has custom status, after.activity.name returns custom status and not the game one is playing, thus one can avoid getting a role. I can't think of a way around this and I couldn't find any other posts related to this. Any help appreciated.

Upvotes: 2

Views: 692

Answers (1)

Der Kek
Der Kek

Reputation: 49

member.activities returns an array with all activites, including current games.

this is what my profile looks like

and this is what member.activities returns

(<CustomActivity name='this is my custom status message for a test' emoji=None>, <Activity type=<ActivityType.playing: 0> name='Code' url=None details='Debugging main.py' application_id=782685898163617802 session_id=None emoji=None>)

you can test it like this in the on_message event if you want: (remember using this event breaks commands, so dont forget to remove it)

@bot.event
async def on_message(message):
    for activity in message.author.activities:
        print(f"    > {activity}")

Upvotes: 1

Related Questions