Skip to content

Commit c5f322e

Browse files
authored
Make edit functions more like discord.py edit functions (#230)
1 parent cfa9f9a commit c5f322e

File tree

2 files changed

+88
-40
lines changed

2 files changed

+88
-40
lines changed

discord_slash/context.py

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -380,33 +380,57 @@ async def edit_origin(self, **fields):
380380
"""
381381
_resp = {}
382382

383-
content = fields.get("content")
384-
if content:
385-
_resp["content"] = str(content)
383+
try:
384+
content = fields["content"]
385+
except KeyError:
386+
pass
387+
else:
388+
if content is not None:
389+
content = str(content)
390+
_resp["content"] = content
391+
392+
try:
393+
components = fields["components"]
394+
except KeyError:
395+
pass
396+
else:
397+
if components is None:
398+
_resp["components"] = []
399+
else:
400+
_resp["components"] = components
401+
402+
try:
403+
embeds = fields["embeds"]
404+
except KeyError:
405+
# Nope
406+
pass
407+
else:
408+
if not isinstance(embeds, list):
409+
raise error.IncorrectFormat("Provide a list of embeds.")
410+
if len(embeds) > 10:
411+
raise error.IncorrectFormat("Do not provide more than 10 embeds.")
412+
_resp["embeds"] = [e.to_dict() for e in embeds]
413+
414+
try:
415+
embed = fields["embed"]
416+
except KeyError:
417+
pass
418+
else:
419+
if "embeds" in _resp:
420+
raise error.IncorrectFormat("You can't use both `embed` and `embeds`!")
421+
422+
if embed is None:
423+
_resp["embeds"] = []
424+
else:
425+
_resp["embeds"] = [embed.to_dict()]
386426

387-
embed = fields.get("embed")
388-
embeds = fields.get("embeds")
389427
file = fields.get("file")
390428
files = fields.get("files")
391-
components = fields.get("components")
392429

393-
if components:
394-
_resp["components"] = components
395-
396-
if embed and embeds:
397-
raise error.IncorrectFormat("You can't use both `embed` and `embeds`!")
398-
if file and files:
430+
if files is not None and file is not None:
399431
raise error.IncorrectFormat("You can't use both `file` and `files`!")
400432
if file:
401433
files = [file]
402-
if embed:
403-
embeds = [embed]
404-
if embeds:
405-
if not isinstance(embeds, list):
406-
raise error.IncorrectFormat("Provide a list of embeds.")
407-
elif len(embeds) > 10:
408-
raise error.IncorrectFormat("Do not provide more than 10 embeds.")
409-
_resp["embeds"] = [x.to_dict() for x in embeds]
410434

411435
allowed_mentions = fields.get("allowed_mentions")
412436
_resp["allowed_mentions"] = (

discord_slash/model.py

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -495,33 +495,57 @@ async def _slash_edit(self, **fields):
495495
"""
496496
_resp = {}
497497

498-
content = fields.get("content")
499-
if content:
500-
_resp["content"] = str(content)
498+
try:
499+
content = fields["content"]
500+
except KeyError:
501+
pass
502+
else:
503+
if content is not None:
504+
content = str(content)
505+
_resp["content"] = content
506+
507+
try:
508+
components = fields["components"]
509+
except KeyError:
510+
pass
511+
else:
512+
if components is None:
513+
_resp["components"] = []
514+
else:
515+
_resp["components"] = components
516+
517+
try:
518+
embeds = fields["embeds"]
519+
except KeyError:
520+
# Nope
521+
pass
522+
else:
523+
if not isinstance(embeds, list):
524+
raise error.IncorrectFormat("Provide a list of embeds.")
525+
if len(embeds) > 10:
526+
raise error.IncorrectFormat("Do not provide more than 10 embeds.")
527+
_resp["embeds"] = [e.to_dict() for e in embeds]
528+
529+
try:
530+
embed = fields["embed"]
531+
except KeyError:
532+
pass
533+
else:
534+
if "embeds" in _resp:
535+
raise error.IncorrectFormat("You can't use both `embed` and `embeds`!")
536+
537+
if embed is None:
538+
_resp["embeds"] = []
539+
else:
540+
_resp["embeds"] = [embed.to_dict()]
501541

502-
embed = fields.get("embed")
503-
embeds = fields.get("embeds")
504542
file = fields.get("file")
505543
files = fields.get("files")
506-
components = fields.get("components")
507544

508-
if components:
509-
_resp["components"] = components
510-
511-
if embed and embeds:
512-
raise error.IncorrectFormat("You can't use both `embed` and `embeds`!")
513-
if file and files:
545+
if files is not None and file is not None:
514546
raise error.IncorrectFormat("You can't use both `file` and `files`!")
515547
if file:
516548
files = [file]
517-
if embed:
518-
embeds = [embed]
519-
if embeds:
520-
if not isinstance(embeds, list):
521-
raise error.IncorrectFormat("Provide a list of embeds.")
522-
elif len(embeds) > 10:
523-
raise error.IncorrectFormat("Do not provide more than 10 embeds.")
524-
_resp["embeds"] = [x.to_dict() for x in embeds]
525549

526550
allowed_mentions = fields.get("allowed_mentions")
527551
_resp["allowed_mentions"] = (

0 commit comments

Comments
 (0)