4
4
from ..error import LibraryException
5
5
from ..models .channel import Channel
6
6
from ..models .message import Message
7
+ from ..models .misc import Snowflake
7
8
from .request import _Request
8
9
from .route import Route
9
10
@@ -312,8 +313,10 @@ async def create_tag(
312
313
self ,
313
314
channel_id : int ,
314
315
name : str ,
316
+ moderated : bool = False ,
315
317
emoji_id : Optional [int ] = None ,
316
318
emoji_name : Optional [str ] = None ,
319
+ reason : Optional [str ] = None ,
317
320
) -> dict :
318
321
"""
319
322
Create a new tag.
@@ -324,25 +327,41 @@ async def create_tag(
324
327
325
328
:param channel_id: Channel ID snowflake.
326
329
:param name: The name of the tag
330
+ :param moderated: Whether the tag can only be assigned to moderators or not. Defaults to ``False``
327
331
:param emoji_id: The ID of the emoji to use for the tag
328
332
:param emoji_name: The name of the emoji to use for the tag
333
+ :param reason: The reason for the creating the tag, if any.
334
+ :return: A Forum tag.
329
335
"""
330
336
331
- _dct = {"name" : name }
337
+ # This *assumes* cache is up-to-date.
338
+
339
+ _channel = self .cache [Channel ].get (Snowflake (channel_id ))
340
+ _tags = [_ ._json for _ in _channel .available_tags ] # list of tags in dict form
341
+
342
+ _dct = {"name" : name , "moderated" : moderated }
332
343
if emoji_id :
333
344
_dct ["emoji_id" ] = emoji_id
334
345
if emoji_name :
335
346
_dct ["emoji_name" ] = emoji_name
336
347
337
- return await self ._req .request (Route ("POST" , f"/channels/{ channel_id } /tags" ), json = _dct )
348
+ _tags .append (_dct )
349
+
350
+ updated_channel = await self .modify_channel (
351
+ channel_id , {"available_tags" : _tags }, reason = reason
352
+ )
353
+ _channel_obj = Channel (** updated_channel , _client = self )
354
+ return _channel_obj .available_tags [- 1 ]._json
338
355
339
356
async def edit_tag (
340
357
self ,
341
358
channel_id : int ,
342
359
tag_id : int ,
343
360
name : str ,
361
+ moderated : Optional [bool ] = None ,
344
362
emoji_id : Optional [int ] = None ,
345
363
emoji_name : Optional [str ] = None ,
364
+ reason : Optional [str ] = None ,
346
365
) -> dict :
347
366
"""
348
367
Update a tag.
@@ -351,28 +370,62 @@ async def edit_tag(
351
370
Can either have an emoji_id or an emoji_name, but not both.
352
371
emoji_id is meant for custom emojis, emoji_name is meant for unicode emojis.
353
372
373
+ The object returns *will* have a different tag ID.
374
+
354
375
:param channel_id: Channel ID snowflake.
355
376
:param tag_id: The ID of the tag to update.
377
+ :param moderated: Whether the tag can only be assigned to moderators or not. Defaults to ``False``
356
378
:param name: The new name of the tag
357
379
:param emoji_id: The ID of the emoji to use for the tag
358
380
:param emoji_name: The name of the emoji to use for the tag
381
+ :param reason: The reason for deleting the tag, if any.
382
+
383
+ :return The updated tag object.
359
384
"""
360
385
361
- _dct = {"name" : name }
386
+ # This *assumes* cache is up-to-date.
387
+
388
+ _channel = self .cache [Channel ].get (Snowflake (channel_id ))
389
+ _tags = [_ ._json for _ in _channel .available_tags ] # list of tags in dict form
390
+
391
+ _old_tag = [tag for tag in _tags if tag ["id" ] == tag_id ][0 ]
392
+
393
+ _tags .remove (_old_tag )
394
+
395
+ _dct = {"name" : name , "tag_id" : tag_id }
396
+ if moderated :
397
+ _dct ["moderated" ] = moderated
362
398
if emoji_id :
363
399
_dct ["emoji_id" ] = emoji_id
364
400
if emoji_name :
365
401
_dct ["emoji_name" ] = emoji_name
366
402
367
- return await self ._req .request (
368
- Route ("PUT" , f"/channels/{ channel_id } /tags/{ tag_id } " ), json = _dct
403
+ _tags .append (_dct )
404
+
405
+ updated_channel = await self .modify_channel (
406
+ channel_id , {"available_tags" : _tags }, reason = reason
369
407
)
408
+ _channel_obj = Channel (** updated_channel , _client = self )
409
+
410
+ self .cache [Channel ].merge (_channel_obj )
411
+
412
+ return [tag for tag in _channel_obj .available_tags if tag .name == name ][0 ]
370
413
371
- async def delete_tag (self , channel_id : int , tag_id : int ) -> None : # wha?
414
+ async def delete_tag (self , channel_id : int , tag_id : int , reason : Optional [ str ] = None ) -> None :
372
415
"""
373
416
Delete a forum tag.
374
417
375
418
:param channel_id: Channel ID snowflake.
376
419
:param tag_id: The ID of the tag to delete
420
+ :param reason: The reason for deleting the tag, if any.
377
421
"""
378
- return await self ._req .request (Route ("DELETE" , f"/channels/{ channel_id } /tags/{ tag_id } " ))
422
+ _channel = self .cache [Channel ].get (Snowflake (channel_id ))
423
+ _tags = [_ ._json for _ in _channel .available_tags ]
424
+
425
+ _old_tag = [tag for tag in _tags if tag ["id" ] == Snowflake (tag_id )][0 ]
426
+
427
+ _tags .remove (_old_tag )
428
+
429
+ request = await self .modify_channel (channel_id , {"available_tags" : _tags }, reason = reason )
430
+
431
+ self .cache [Channel ].merge (Channel (** request , _client = self ))
0 commit comments