Skip to content

Commit 9f3e473

Browse files
feat(message): add helper methods to Embed (#531)
* feat Embed: Added Embed Methods * ci: correct from checks. * Added docstrings and fixed method parameters * ci: correct from checks. * Added -> None to each methods * fixed merge issue * ci: correct from checks. * Added int specification at remove_field() Co-authored-by: = <=> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 7897743 commit 9f3e473

File tree

3 files changed

+259
-65
lines changed

3 files changed

+259
-65
lines changed

.pre-commit-config.yaml

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,59 @@
1-
repos:
2-
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v4.1.0
4-
hooks:
5-
- id: requirements-txt-fixer
6-
name: Requirements
7-
types: [file]
8-
exclude_types: ['image']
9-
- id: debug-statements
10-
name: Debugging
11-
language: python
12-
types: [file, python]
13-
exclude_types: ['image']
14-
- id: trailing-whitespace
15-
name: Trailing Whitespace
16-
language: python
17-
types: [file]
18-
exclude_types: ['image']
19-
- id: end-of-file-fixer
20-
name: EOF Newlines
21-
language: python
22-
types: [file]
23-
exclude_types: ['image']
24-
- id: check-yaml
25-
name: YAML Structure
26-
language: python
27-
- id: check-toml
28-
name: TOML Structure
29-
- id: check-merge-conflict
30-
name: Merge Conflicts
31-
- repo: https://github.com/psf/black
32-
rev: 22.1.0
33-
hooks:
34-
- id: black
35-
name: Black Formatting
36-
language: python
37-
types: [file, python]
38-
args: [--line-length=100]
39-
- repo: https://github.com/PyCQA/flake8
40-
rev: 4.0.1
41-
hooks:
42-
- id: flake8
43-
name: flake8 Formatting
44-
language: python
45-
types: [file, python]
46-
args: [--max-line-length=100, --ignore=E203 E301 E302 E501 E402 E704 W503 W504]
47-
- repo: https://github.com/pycqa/isort
48-
rev: 5.10.1
49-
hooks:
50-
- id: isort
51-
name: isort Formatting
52-
language: python
53-
types: [file, python]
54-
ci:
55-
autoupdate_branch: "unstable"
56-
autofix_prs: true
57-
autoupdate_commit_msg: "ci: weekly check."
58-
autoupdate_schedule: weekly
59-
autofix_commit_msg: "ci: correct from checks."
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.1.0
4+
hooks:
5+
- id: requirements-txt-fixer
6+
name: Requirements
7+
types: [file]
8+
exclude_types: ['image']
9+
- id: debug-statements
10+
name: Debugging
11+
language: python
12+
types: [file, python]
13+
exclude_types: ['image']
14+
- id: trailing-whitespace
15+
name: Trailing Whitespace
16+
language: python
17+
types: [file]
18+
exclude_types: ['image']
19+
- id: end-of-file-fixer
20+
name: EOF Newlines
21+
language: python
22+
types: [file]
23+
exclude_types: ['image']
24+
- id: check-yaml
25+
name: YAML Structure
26+
language: python
27+
- id: check-toml
28+
name: TOML Structure
29+
- id: check-merge-conflict
30+
name: Merge Conflicts
31+
- repo: https://github.com/psf/black
32+
rev: 22.1.0
33+
hooks:
34+
- id: black
35+
name: Black Formatting
36+
language: python
37+
types: [file, python]
38+
args: [--line-length=100]
39+
- repo: https://github.com/PyCQA/flake8
40+
rev: 4.0.1
41+
hooks:
42+
- id: flake8
43+
name: flake8 Formatting
44+
language: python
45+
types: [file, python]
46+
args: [--max-line-length=100, --ignore=E203 E301 E302 E501 E402 E704 W503 W504]
47+
- repo: https://github.com/pycqa/isort
48+
rev: 5.10.1
49+
hooks:
50+
- id: isort
51+
name: isort Formatting
52+
language: python
53+
types: [file, python]
54+
ci:
55+
autoupdate_branch: "unstable"
56+
autofix_prs: true
57+
autoupdate_commit_msg: "ci: weekly check."
58+
autoupdate_schedule: weekly
59+
autofix_commit_msg: "ci: correct from checks."

interactions/api/models/message.py

Lines changed: 190 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -902,11 +902,195 @@ def __init__(self, **kwargs):
902902
if self.footer:
903903
self._json.update({"footer": self.footer._json})
904904

905-
if self.video:
906-
self._json.update({"video": self.video._json})
905+
def add_field(self, name: str, value: str, inline: Optional[bool] = False) -> None:
906+
"""
907+
Adds a field to the embed
908+
909+
:param name: The name of the field
910+
:type name: str
911+
:param value: The value of the field
912+
:type value: str
913+
:param inline?: if the field is in the same line as the previous one
914+
:type inline?: Optional[bool]
915+
"""
916+
917+
if self.fields is None:
918+
self.fields = []
919+
920+
self.fields.append(EmbedField(name=name, value=value, inline=inline))
921+
self._json.update({"fields": [field._json for field in self.fields]})
922+
923+
def clear_fields(self) -> None:
924+
"""
925+
Clears all the fields of the embed
926+
"""
927+
928+
self.fields = []
929+
self._json.update({"fields": []})
930+
931+
def insert_field_at(
932+
self, index: int, name: str = None, value: str = None, inline: Optional[bool] = False
933+
) -> None:
934+
"""
935+
Inserts a field in the embed at the specified index
936+
937+
:param index: The new field's index
938+
:type index: int
939+
:param name: The name of the field
940+
:type name: str
941+
:param value: The value of the field
942+
:type value: str
943+
:param inline?: if the field is in the same line as the previous one
944+
:type inline?: Optional[bool]
945+
"""
946+
947+
try:
948+
self.fields.insert(index, EmbedField(name=name, value=value, inline=inline))
949+
950+
except AttributeError:
951+
self.fields = [EmbedField(name=name, value=value, inline=inline)]
952+
953+
self._json.update({"fields": [field._json for field in self.fields]})
954+
955+
def set_field_at(
956+
self, index: int, name: str, value: str, inline: Optional[bool] = False
957+
) -> None:
958+
"""
959+
Overwrites the field in the embed at the specified index
960+
961+
:param index: The new field's index
962+
:type index: int
963+
:param name: The name of the field
964+
:type name: str
965+
:param value: The value of the field
966+
:type value: str
967+
:param inline?: if the field is in the same line as the previous one
968+
:type inline?: Optional[bool]
969+
"""
970+
971+
try:
972+
self.fields[index] = EmbedField(name=name, value=value, inline=inline)
973+
self._json.update({"fields": [field._json for field in self.fields]})
974+
975+
except AttributeError:
976+
raise AttributeError("No fields found in Embed")
977+
978+
except IndexError:
979+
raise IndexError("No fields at this index")
980+
981+
def remove_field(self, index: int) -> None:
982+
"""
983+
Remove field at the specified index
984+
985+
:param index: The new field's index
986+
:type index: int
987+
"""
988+
989+
try:
990+
self.fields.pop(index)
991+
self._json.update({"fields": [field._json for field in self.fields]})
992+
993+
except AttributeError:
994+
raise AttributeError("No fields found in Embed")
995+
996+
except IndexError:
997+
raise IndexError("Field not Found at index")
998+
999+
def remove_author(self) -> None:
1000+
"""
1001+
Removes the embed's author
1002+
"""
1003+
1004+
try:
1005+
del self.author
1006+
self._json.update({"author": None})
1007+
except AttributeError:
1008+
pass
1009+
1010+
def set_author(
1011+
self,
1012+
name: str,
1013+
url: Optional[str] = None,
1014+
icon_url: Optional[str] = None,
1015+
proxy_icon_url: Optional[str] = None,
1016+
) -> None:
1017+
"""
1018+
Sets the embed's author
1019+
1020+
:param name: The name of the author
1021+
:type name: str
1022+
:param url?: Url of author
1023+
:type url?: Optional[str]
1024+
:param icon_url?: Url of author icon (only supports http(s) and attachments)
1025+
:type icon_url?: Optional[str]
1026+
:param proxy_icon_url?: A proxied url of author icon
1027+
:type proxy_icon_url?: Optional[str]
1028+
"""
1029+
1030+
self.author = EmbedAuthor(
1031+
name=name, url=url, icon_url=icon_url, proxy_icon_url=proxy_icon_url
1032+
)
1033+
self._json.update({"author": self.author._json})
9071034

908-
if self.image:
909-
self._json.update({"image": self.image._json})
1035+
def set_footer(
1036+
self, text: str, icon_url: Optional[str] = None, proxy_icon_url: Optional[str] = None
1037+
) -> None:
1038+
"""
1039+
Sets the embed's footer
1040+
1041+
:param text: The text of the footer
1042+
:type text: str
1043+
:param icon_url?: Url of footer icon (only supports http(s) and attachments)
1044+
:type icon_url?: Optional[str]
1045+
:param proxy_icon_url?: A proxied url of footer icon
1046+
:type proxy_icon_url?: Optional[str]
1047+
"""
1048+
1049+
self.footer = EmbedFooter(text=text, icon_url=icon_url, proxy_icon_url=proxy_icon_url)
1050+
self._json.update({"footer": self.footer._json})
1051+
1052+
def set_image(
1053+
self,
1054+
url: str,
1055+
proxy_url: Optional[str] = None,
1056+
height: Optional[int] = None,
1057+
width: Optional[int] = None,
1058+
) -> None:
1059+
"""
1060+
Sets the embed's image
1061+
1062+
:param url: Url of the image
1063+
:type url: str
1064+
:param proxy_url?: A proxied url of the image
1065+
:type proxy_url?: Optional[str]
1066+
:param height?: The image's height
1067+
:type height?: Optional[int]
1068+
:param width?: The image's width
1069+
:type width?: Optional[int]
1070+
"""
1071+
1072+
self.image = EmbedImageStruct(url=url, proxy_url=proxy_url, height=height, width=width)
1073+
self._json.update({"image": self.image._json})
1074+
1075+
def set_thumbnail(
1076+
self,
1077+
url: str,
1078+
proxy_url: Optional[str] = None,
1079+
height: int = None,
1080+
width: Optional[str] = None,
1081+
) -> None:
1082+
"""
1083+
Sets the embed's thumbnail
1084+
1085+
:param url: Url of the thumbnail
1086+
:type url: str
1087+
:param proxy_url?: A proxied url of the thumbnail
1088+
:type proxy_url?: Optional[str]
1089+
:param height?: The thumbnail's height
1090+
:type height?: Optional[int]
1091+
:param width?: The thumbnail's width
1092+
:type width?: Optional[int]
1093+
"""
9101094

911-
if self.thumbnail:
912-
self._json.update({"thumbnail": self.thumbnail._json})
1095+
self.thumbnail = EmbedImageStruct(url=url, proxy_url=proxy_url, height=height, width=width)
1096+
self._json.update({"thumbnail": self.thumbnail._json})

interactions/api/models/message.pyi

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,13 @@ class Embed(DictSerializerMixin):
244244
author: Optional[EmbedAuthor]
245245
fields: Optional[List[EmbedField]]
246246
def __init__(self, **kwargs): ...
247+
def add_field(self, name: str, value: str, inline: Optional[bool] = False) -> None: ...
248+
def clear_fields(self) -> None: ...
249+
def insert_field_at(self, index: int, name: str, value: str, inline: Optional[bool] = False) -> None: ...
250+
def set_field_at(self, index: int, name: str, value: str, inline: Optional[bool] = False) -> None: ...
251+
def remove_field(self, index: int) -> None: ...
252+
def remove_author(self) -> None: ...
253+
def set_author(self, name: str, url: Optional[str] = None, icon_url: Optional[str] = None, proxy_icon_url: Optional[str] = None) -> None: ...
254+
def set_footer(self, text: str, icon_url: Optional[str] = None, proxy_icon_url: Optional[str] = None) -> None: ...
255+
def set_image(self, url: str, proxy_url: Optional[str] = None, height: Optional[int] = None, width: Optional[int] = None) -> None: ...
256+
def set_thumbnail(self, url: str, proxy_url: Optional[str] = None, height: Optional[int] = None, width: Optional[int] = None) -> None: ...

0 commit comments

Comments
 (0)