Skip to content

Commit 2b2b36c

Browse files
committed
Add moderation endpoints
ban, timeout, unban and send_whisper endpoints and methods added.
1 parent 291bd74 commit 2b2b36c

File tree

4 files changed

+216
-0
lines changed

4 files changed

+216
-0
lines changed

docs/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ Master
1515
- :func:`twitchio.PartialUser.remove_channel_moderator`
1616
- :func:`twitchio.PartialUser.start_raid`
1717
- :func:`twitchio.PartialUser.cancel_raid`
18+
- :func:`twitchio.PartialUser.ban_user`
19+
- :func:`twitchio.PartialUser.timeout_user`
20+
- :func:`twitchio.PartialUser.unban_user`
21+
- :func:`twitchio.PartialUser.send_whisper`
1822
- Added following new Client methods:
1923
- :func:`~twitchio.Client.fetch_chatters_colors`
2024
- :func:`~twitchio.Client.update_chatter_color`

twitchio/http.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,3 +1036,30 @@ async def post_raid(self, token: str, from_broadcaster_id: str, to_broadcaster_i
10361036
async def delete_raid(self, token: str, broadcaster_id: str):
10371037
q = [("broadcaster_id", broadcaster_id)]
10381038
return await self.request(Route("DELETE", "raids", query=q, token=token))
1039+
1040+
async def post_ban_timeout_user(
1041+
self,
1042+
token: str,
1043+
broadcaster_id: str,
1044+
moderator_id: str,
1045+
user_id: str,
1046+
reason: str,
1047+
duration: Optional[int] = None,
1048+
):
1049+
q = [("broadcaster_id", broadcaster_id), ("moderator_id", moderator_id)]
1050+
body = {"data": {"user_id": user_id, "reason": reason}}
1051+
if duration:
1052+
if duration < 1 or duration > 1209600:
1053+
raise ValueError("Duration must be between 1 and 1209600 seconds")
1054+
body["data"]["duration"] = str(duration)
1055+
return await self.request(Route("POST", "moderation/bans", query=q, body=body, token=token))
1056+
1057+
async def delete_ban_timeout_user(
1058+
self,
1059+
token: str,
1060+
broadcaster_id: str,
1061+
moderator_id: str,
1062+
user_id: str,
1063+
):
1064+
q = [("broadcaster_id", broadcaster_id), ("moderator_id", moderator_id), ("user_id", user_id)]
1065+
return await self.request(Route("DELETE", "moderation/bans", query=q, token=token))

twitchio/models.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,8 @@ def __repr__(self):
348348

349349
class BanEvent:
350350
"""
351+
This has been deprecated.
352+
351353
Represents a user being banned from a channel.
352354
353355
Attributes
@@ -1605,3 +1607,62 @@ def __init__(self, data: dict):
16051607

16061608
def __repr__(self):
16071609
return f"<Raid created_at={self.created_at} is_mature={self.is_mature}>"
1610+
1611+
1612+
class Ban:
1613+
"""
1614+
Represents a ban for a broadcaster / channel
1615+
1616+
Attributes
1617+
-----------
1618+
broadcaster: :class:`~twitchio.PartialUser`
1619+
The broadcaster whose chat room the user was banned from chatting in.
1620+
moderator: :class:`~twitchio.PartialUser`
1621+
The moderator that banned the user.
1622+
user: :class:`~twitchio.PartialUser`
1623+
The user that was banned.
1624+
created_at: :class:`datetime.datetime`
1625+
Date and time of when the ban was created.
1626+
"""
1627+
1628+
__slots__ = ("broadcaster", "moderator", "user", "created_at")
1629+
1630+
def __init__(self, http: "TwitchHTTP", data: dict):
1631+
self.broadcaster = PartialUser(http, data["broadcaster_id"], None)
1632+
self.moderator = PartialUser(http, data["moderator_id"], None)
1633+
self.user = PartialUser(http, data["user_id"], None)
1634+
self.created_at: datetime.datetime = parse_timestamp(data["created_at"])
1635+
1636+
def __repr__(self):
1637+
return f"<Ban broadcaster={self.broadcaster} user={self.user} created_at={self.created_at}>"
1638+
1639+
1640+
class Timeout:
1641+
"""
1642+
Represents a timeout for a broadcaster / channel
1643+
1644+
Attributes
1645+
-----------
1646+
broadcaster: :class:`~twitchio.PartialUser`
1647+
The broadcaster whose chat room the user was timed out from chatting in.
1648+
moderator: :class:`~twitchio.PartialUser`
1649+
The moderator that timed the user out.
1650+
user: :class:`~twitchio.PartialUser`
1651+
The user that was timed out.
1652+
created_at: :class:`datetime.datetime`
1653+
Date and time of when the timeout was created.
1654+
end_time: :class:`datetime.datetime`
1655+
Date and time of when the timeout will end.
1656+
"""
1657+
1658+
__slots__ = ("broadcaster", "moderator", "user", "created_at", "end_time")
1659+
1660+
def __init__(self, http: "TwitchHTTP", data: dict):
1661+
self.broadcaster = PartialUser(http, data["broadcaster_id"], None)
1662+
self.moderator = PartialUser(http, data["moderator_id"], None)
1663+
self.user = PartialUser(http, data["user_id"], None)
1664+
self.created_at: datetime.datetime = parse_timestamp(data["created_at"])
1665+
self.end_time: datetime.datetime = parse_timestamp(data["end_time"])
1666+
1667+
def __repr__(self):
1668+
return f"<Timeout broadcaster={self.broadcaster} user={self.user} created_at={self.created_at} end_time={self.end_time}>"

twitchio/user.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ async def fetch_bans(self, token: str, userids: List[Union[str, int]] = None) ->
368368
async def fetch_ban_events(self, token: str, userids: List[int] = None):
369369
"""|coro|
370370
371+
This has been deprecated and will be removed in a future release.
372+
371373
Fetches ban/unban events from the User's channel.
372374
373375
Parameters
@@ -1301,6 +1303,128 @@ async def cancel_raid(self, token: str):
13011303

13021304
await self._http.delete_raid(broadcaster_id=str(self.id), token=token)
13031305

1306+
async def ban_user(self, token: str, moderator_id: int, user_id, reason: str):
1307+
"""|coro|
1308+
1309+
Bans a user from the channel/broadcaster.
1310+
1311+
Parameters
1312+
-----------
1313+
token: :class:`str`
1314+
An oauth user access token with the ``moderator:manage:banned_users`` scope
1315+
moderator_id: :class:`int`
1316+
The ID of a user that has permission to moderate the broadcaster's chat room.
1317+
If the broadcaster wants to ban the user set this parameter to the broadcaster's ID.
1318+
user_id: :class:`int`
1319+
The ID of the user to ban.
1320+
reason: :class:`str`
1321+
The reason for the ban.
1322+
1323+
Returns
1324+
--------
1325+
:class:`twitchio.Ban`
1326+
"""
1327+
from .models import Ban
1328+
1329+
data = await self._http.post_ban_timeout_user(
1330+
broadcaster_id=str(self.id),
1331+
moderator_id=str(moderator_id),
1332+
user_id=str(user_id),
1333+
reason=reason,
1334+
token=token,
1335+
)
1336+
return Ban(self._http, data[0])
1337+
1338+
async def timeout_user(self, token: str, moderator_id: int, user_id: int, duration: int, reason: str):
1339+
"""|coro|
1340+
1341+
Timeouts a user from the channel/broadcaster.
1342+
1343+
Parameters
1344+
-----------
1345+
token: :class:`str`
1346+
An oauth user access token with the ``moderator:manage:banned_users`` scope
1347+
moderator_id: :class:`int`
1348+
The ID of a user that has permission to moderate the broadcaster's chat room.
1349+
If the broadcaster wants to timeout the user set this parameter to the broadcaster's ID.
1350+
duration: :class:`int`
1351+
The duration of the timeout in seconds.
1352+
The minimum timeout is 1 second and the maximum is 1,209,600 seconds (2 weeks).
1353+
To end a user's timeout early, set this field to 1, or send an Unban user request.
1354+
reason: :class:`str`
1355+
The reason for the timeout.
1356+
1357+
Returns
1358+
--------
1359+
:class:`twitchio.Timeout`
1360+
"""
1361+
from .models import Timeout
1362+
1363+
data = await self._http.post_ban_timeout_user(
1364+
broadcaster_id=str(self.id),
1365+
moderator_id=str(moderator_id),
1366+
user_id=str(user_id),
1367+
duration=duration,
1368+
reason=reason,
1369+
token=token,
1370+
)
1371+
return Timeout(self._http, data[0])
1372+
1373+
async def unban_user(self, token: str, moderator_id: int, user_id):
1374+
"""|coro|
1375+
1376+
Unbans a user or removes a timeout from the channel/broadcaster.
1377+
1378+
Parameters
1379+
-----------
1380+
token: :class:`str`
1381+
An oauth user access token with the ``moderator:manage:banned_users`` scope
1382+
moderator_id: :class:`int`
1383+
The ID of a user that has permission to moderate the broadcaster's chat room.
1384+
If the broadcaster wants to ban the user set this parameter to the broadcaster's ID.
1385+
user_id: :class:`int`
1386+
The ID of the user to unban.
1387+
1388+
Returns
1389+
--------
1390+
None
1391+
"""
1392+
1393+
await self._http.delete_ban_timeout_user(
1394+
broadcaster_id=str(self.id),
1395+
moderator_id=str(moderator_id),
1396+
user_id=str(user_id),
1397+
token=token,
1398+
)
1399+
1400+
async def send_whisper(self, token: str, user_id: int, message: str):
1401+
"""|coro|
1402+
1403+
Sends a whisper to a user.
1404+
Important Notes:
1405+
- The user sending the whisper must have a verified phone number.
1406+
- The API may silently drop whispers that it suspects of violating Twitch policies.
1407+
- You may whisper to a maximum of 40 unique recipients per day. Within the per day limit.
1408+
- You may whisper a maximum of 3 whispers per second and a maximum of 100 whispers per minute.
1409+
1410+
Parameters
1411+
-----------
1412+
token: :class:`str`
1413+
An oauth user token with the ``user:manage:whispers`` scope.
1414+
user_id: :class:`int`
1415+
The ID of the user to send the whisper to.
1416+
message: :class:`str`
1417+
The message to send.
1418+
500 characters if the user you're sending the message to hasn't whispered you before.
1419+
10,000 characters if the user you're sending the message to has whispered you before.
1420+
1421+
Returns
1422+
--------
1423+
None
1424+
"""
1425+
1426+
await self._http.post_whisper(token=token, from_user_id=str(self.id), to_user_id=str(user_id), message=message)
1427+
13041428

13051429
class BitLeaderboardUser(PartialUser):
13061430

0 commit comments

Comments
 (0)