Skip to content

Commit 0633031

Browse files
committed
Add PartialUser.create_custom_reward
Added a PartialUser method that allows creation of a custom reward
1 parent 80bb399 commit 0633031

File tree

3 files changed

+83
-15
lines changed

3 files changed

+83
-15
lines changed

twitchio/http.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -346,14 +346,14 @@ async def create_reward(
346346
broadcaster_id: int,
347347
title: str,
348348
cost: int,
349-
prompt: str = None,
350-
is_enabled: bool = True,
351-
background_color: str = None,
352-
user_input_required: bool = False,
353-
max_per_stream: int = None,
354-
max_per_user: int = None,
355-
global_cooldown: int = None,
356-
fufill_immediatly: bool = False,
349+
prompt: Optional[str] = None,
350+
is_enabled: Optional[bool] = True,
351+
background_color: Optional[str] = None,
352+
user_input_required: Optional[bool] = False,
353+
max_per_stream: Optional[int] = None,
354+
max_per_user: Optional[int] = None,
355+
global_cooldown: Optional[int] = None,
356+
fufill_immediatly: Optional[bool] = False,
357357
):
358358
params = [("broadcaster_id", str(broadcaster_id))]
359359
data = {
@@ -366,10 +366,10 @@ async def create_reward(
366366
}
367367
if max_per_stream:
368368
data["max_per_stream"] = max_per_stream
369-
data["max_per_stream_enabled"] = True
369+
data["is_max_per_stream_enabled"] = True
370370
if max_per_user:
371371
data["max_per_user_per_stream"] = max_per_user
372-
data["max_per_user_per_stream_enabled"] = True
372+
data["is_max_per_user_per_stream_enabled"] = True
373373
if background_color:
374374
data["background_color"] = background_color
375375
if global_cooldown:

twitchio/rewards.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
if TYPE_CHECKING:
3434
from .http import TwitchHTTP
3535
from .user import PartialUser
36-
37-
3836
__all__ = "CustomReward", "CustomRewardRedemption"
3937

4038

@@ -73,7 +71,6 @@ def __init__(self, http: "TwitchHTTP", obj: dict, channel: "PartialUser"):
7371
self._broadcaster_id = obj["broadcaster_id"]
7472
except KeyError:
7573
self._broadcaster_id = obj["channel_id"]
76-
7774
self.id = obj["id"]
7875
self.image = obj["image"]["url_1x"] if obj["image"] else obj["default_image"]["url_1x"]
7976
self.background_color = obj["background_color"]
@@ -106,7 +103,6 @@ def __init__(self, http: "TwitchHTTP", obj: dict, channel: "PartialUser"):
106103
obj["global_cooldown"]["is_enabled"],
107104
obj["global_cooldown"]["global_cooldown_seconds"],
108105
)
109-
110106
self.paused = obj["is_paused"]
111107
self.in_stock = obj["is_in_stock"]
112108
self.redemptions_skip_queue = obj["should_redemptions_skip_request_queue"]
@@ -194,7 +190,6 @@ async def edit(
194190
if reward["id"] == self.id:
195191
self.__init__(self._http, reward, self._channel)
196192
break
197-
198193
return self
199194

200195
async def delete(self, token: str):
@@ -263,6 +258,7 @@ async def get_redemptions(self, token: str, status: str, sort: str = "OLDEST", f
263258
def __repr__(self):
264259
return f"<CustomReward id={self.id} title={self.title} cost={self.cost}>"
265260

261+
266262
class CustomRewardRedemption:
267263

268264
__slots__ = "_http", "_broadcaster_id", "id", "user_id", "user_name", "input", "status", "redeemed_at", "reward"

twitchio/user.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,78 @@ async def get_custom_rewards(
178178
self._cached_rewards = time.monotonic(), values
179179
return values
180180

181+
async def create_custom_reward(
182+
self,
183+
token: str,
184+
title: str,
185+
cost: int,
186+
prompt: Optional[str] = None,
187+
enabled: Optional[bool] = True,
188+
background_color: Optional[str] = None,
189+
input_required: Optional[bool] = False,
190+
max_per_stream: Optional[int] = None,
191+
max_per_user_per_stream: Optional[int] = None,
192+
global_cooldown: Optional[int] = None,
193+
redemptions_skip_queue: Optional[bool] = False,
194+
) -> "CustomReward":
195+
"""|coro|
196+
197+
Creates a custom reward for the user.
198+
199+
Parameters
200+
-----------
201+
token: :class:`str`
202+
An oauth token with the user:edit:broadcast scope
203+
title: :class:`str`
204+
The title of the reward
205+
cost: :class:`int`
206+
The cost of the reward
207+
prompt: Optional[:class:`str`]
208+
The prompt for the reward. Defaults to None
209+
enabled: Optional[:class:`bool`]
210+
Whether the reward is enabled. Defaults to True
211+
background_color: Optional[:class:`str`]
212+
The background color of the reward. Defaults to None
213+
input_required: Optional[:class:`bool`]
214+
Whether the reward requires input. Defaults to False
215+
max_per_stream: Optional[:class:`int`]
216+
The maximum number of times the reward can be redeemed per stream. Defaults to None
217+
max_per_user_per_stream: Optional[:class:`int`]
218+
The maximum number of times the reward can be redeemed per user per stream. Defaults to None
219+
global_cooldown: Optional[:class:`int`]
220+
The global cooldown of the reward. Defaults to None
221+
redemptions_skip_queue: Optional[:class:`bool`]
222+
Whether the reward skips the queue when redeemed. Defaults to False
223+
"""
224+
try:
225+
data = await self._http.create_reward(
226+
token,
227+
self.id,
228+
title,
229+
cost,
230+
prompt,
231+
enabled,
232+
background_color,
233+
input_required,
234+
max_per_stream,
235+
max_per_user_per_stream,
236+
global_cooldown,
237+
redemptions_skip_queue,
238+
)
239+
return CustomReward(self._http, data[0], self)
240+
except Unauthorized as error:
241+
raise Unauthorized("The given token is invalid", "", 401) from error
242+
except HTTPException as error:
243+
status = error.args[2]
244+
if status == 403:
245+
raise HTTPException(
246+
"The custom reward was created by a different application, or channel points are "
247+
"not available for the broadcaster (403)",
248+
error.args[1],
249+
403,
250+
) from error
251+
raise
252+
181253
async def fetch_bits_leaderboard(
182254
self, token: str, period: str = "all", user_id: int = None, started_at: datetime.datetime = None
183255
) -> "BitsLeaderboard":

0 commit comments

Comments
 (0)