How do you get a list of all discord players?



  • There's a piece of code:

    @commands.command()        
    async def gameinfo(self, ctx):
        game_list = []
        for member in ctx.guild.members:
            game_list += member.activities
            print(game_list)
    

    Turns him out:

    [<CustomActivity name='test' emoji=None>, <Game name='Visual Studio Code'>]
    ...
    

    We just need to get out. Game name♪ But when you call... print(game_list['Game name'])makes a mistake:

    TypeError: list indices must be integers or slices, not str
    

    If an index is used, print(game_list[1]), then:

    IndexError: list index out of range
    

    The essence of the team, we need to collect the activity status from all discord users and release the number online in a certain game. Thank you for any damage!



  • When you write:

    print(game_list[1])
    

    And you make a mistake:

    IndexError: list index out of range
    

    This indicates that there is no element on the list with index 1, the list consists of one element with index 0.

    Try that.

    for member in ctx.guild.members:
            game_list.append(member.activities)
            print(game_list)
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2