talkomatic.py 0.1.2
An easy-to-use Python versatile wrapper for the Talkomatic Bot interface ensuring best practices for making bots, and handling the REST API.
 
Loading...
Searching...
No Matches
user.py
Go to the documentation of this file.
1from dataclasses import dataclass
2
3
4@dataclass
5class User:
6 """
7 A class representing a user in a lobby.
8
9 In some places, "id-only users" are used, which are users that only have an ID
10 and no username or location attached. These are where the original Talkomatic
11 API only specifies a user ID, and not a username or location. You can still
12 try retrieving the username and location from the bot if the user is stored
13 in the bot's currently online user database, but this is not guaranteed.
14
15 Composed of:
16 - id: The user's raw ID in web safe Base64 encoding.
17 - username: The user's username. (doesn't apply if the user is a "id-only user")
18 - location: The user's location. (doesn't apply if the user is a "id-only user")
19 - id_only: Whether the user is an "id-only user".
20 """
21
22 id: str
23 username: str
24 location: str
25 id_only: bool
26
27 @classmethod
28 def from_raw_json(cls, data: dict, id_only: bool = False) -> "User":
29 return cls(
30 id = data["id" if "id" in data else "userId"],
31 username = data["username"],
32 location = data["location"],
33 id_only = id_only
34 )
35
36 @classmethod
37 def from_id_only(cls, id: str) -> "User":
38 return cls.from_raw_json({"id": id, "username": "", "location": ""}, id_only = True)
39
40 def __eq__(self, other: "User") -> bool:
41 if not isinstance(other, User):
42 return False
43
44 return self.id == other.id
45
46 def __ne__(self, other: "User") -> bool:
47 return not self.__eq__(other)
48
49 def __str__(self) -> str:
50 return f'User "{self.username} / {self.location}" with id: {self.id}{" (id-only)" if self.id_only else ""}'
51
52 def __hash__(self) -> int:
53 return hash(self.id)
54
55__all__ = ["User"]
bool __eq__(self, "User" other)
Definition user.py:40
bool __ne__(self, "User" other)
Definition user.py:46
"User" from_id_only(cls, str id)
Definition user.py:37
"User" from_raw_json(cls, dict data, bool id_only=False)
Definition user.py:28