@@ -299,6 +299,9 @@ def __init__(self, **kwargs):
299
299
)
300
300
self .thread = Channel (** self .thread ) if self ._json .get ("thread" ) else None
301
301
302
+ def __repr__ (self ) -> str :
303
+ return self .content
304
+
302
305
async def get_channel (self ) -> Channel :
303
306
"""
304
307
Gets the channel where the message was sent.
@@ -854,6 +857,15 @@ class EmbedImageStruct(DictSerializerMixin):
854
857
def __init__ (self , ** kwargs ):
855
858
super ().__init__ (** kwargs )
856
859
860
+ def __setattr__ (self , key , value ) -> None :
861
+ super ().__setattr__ (key , value )
862
+ if key != "_json" and (key not in self ._json or value != self ._json .get (key )):
863
+ if value is not None and value is not MISSING :
864
+ self ._json .update ({key : value })
865
+
866
+ elif value is None and key in self ._json .keys ():
867
+ del self ._json [key ]
868
+
857
869
858
870
class EmbedProvider (DictSerializerMixin ):
859
871
"""
@@ -868,6 +880,15 @@ class EmbedProvider(DictSerializerMixin):
868
880
def __init__ (self , ** kwargs ):
869
881
super ().__init__ (** kwargs )
870
882
883
+ def __setattr__ (self , key , value ) -> None :
884
+ super ().__setattr__ (key , value )
885
+ if key != "_json" and (key not in self ._json or value != self ._json .get (key )):
886
+ if value is not None and value is not MISSING :
887
+ self ._json .update ({key : value })
888
+
889
+ elif value is None and key in self ._json .keys ():
890
+ del self ._json [key ]
891
+
871
892
872
893
class EmbedAuthor (DictSerializerMixin ):
873
894
"""
@@ -892,6 +913,15 @@ class EmbedAuthor(DictSerializerMixin):
892
913
def __init__ (self , ** kwargs ):
893
914
super ().__init__ (** kwargs )
894
915
916
+ def __setattr__ (self , key , value ) -> None :
917
+ super ().__setattr__ (key , value )
918
+ if key != "_json" and (key not in self ._json or value != self ._json .get (key )):
919
+ if value is not None and value is not MISSING :
920
+ self ._json .update ({key : value })
921
+
922
+ elif value is None and key in self ._json .keys ():
923
+ del self ._json [key ]
924
+
895
925
896
926
class EmbedFooter (DictSerializerMixin ):
897
927
"""
@@ -915,6 +945,15 @@ class EmbedFooter(DictSerializerMixin):
915
945
def __init__ (self , ** kwargs ):
916
946
super ().__init__ (** kwargs )
917
947
948
+ def __setattr__ (self , key , value ) -> None :
949
+ super ().__setattr__ (key , value )
950
+ if key != "_json" and (key not in self ._json or value != self ._json .get (key )):
951
+ if value is not None and value is not MISSING :
952
+ self ._json .update ({key : value })
953
+
954
+ elif value is None and key in self ._json .keys ():
955
+ del self ._json [key ]
956
+
918
957
919
958
class EmbedField (DictSerializerMixin ):
920
959
"""
@@ -940,6 +979,15 @@ class EmbedField(DictSerializerMixin):
940
979
def __init__ (self , ** kwargs ):
941
980
super ().__init__ (** kwargs )
942
981
982
+ def __setattr__ (self , key , value ) -> None :
983
+ super ().__setattr__ (key , value )
984
+ if key != "_json" and (key not in self ._json or value != self ._json .get (key )):
985
+ if value is not None and value is not MISSING :
986
+ self ._json .update ({key : value })
987
+
988
+ elif value is None and key in self ._json .keys ():
989
+ del self ._json [key ]
990
+
943
991
944
992
class Embed (DictSerializerMixin ):
945
993
"""
@@ -1037,30 +1085,36 @@ def __init__(self, **kwargs):
1037
1085
else None
1038
1086
)
1039
1087
1040
- # TODO: Complete partial fix.
1088
+ # ( Complete partial fix.)
1041
1089
# The issue seems to be that this itself is not updating
1042
1090
# JSON result correctly. After numerous attempts I seem to
1043
1091
# have the attribute to do it, but _json won't budge at all.
1044
1092
# a genexpr is a poor way to go about this, but I know later
1045
1093
# on we'll be refactoring this anyhow. What the fuck is breaking
1046
1094
# it?
1047
- if self .fields :
1048
- self ._json .update ({"fields" : [field ._json for field in self .fields ]})
1049
-
1050
- if self .author :
1051
- self ._json .update ({"author" : self .author ._json })
1052
-
1053
- if self .footer :
1054
- self ._json .update ({"footer" : self .footer ._json })
1055
1095
1056
- if self .thumbnail :
1057
- self ._json .update ({"thumbnail" : self .thumbnail ._json })
1096
+ # the __setattr__ method fixes this issue :)
1058
1097
1059
- if self .image :
1060
- self ._json .update ({"image" : self .image ._json })
1061
-
1062
- if self .video :
1063
- self ._json .update ({"video" : self .video ._json })
1098
+ def __setattr__ (self , key , value ) -> None :
1099
+ super ().__setattr__ (key , value )
1100
+ if key != "_json" and (
1101
+ key not in self ._json
1102
+ or (
1103
+ value != self ._json .get (key )
1104
+ or not isinstance (value , dict )
1105
+ # we don't need this instance check in components because serialisation works for them
1106
+ )
1107
+ ):
1108
+ if value is not None and value is not MISSING :
1109
+ try :
1110
+ value = [val ._json for val in value ] if isinstance (value , list ) else value ._json
1111
+ except AttributeError :
1112
+ if isinstance (value , datetime ):
1113
+ value = value .isoformat ()
1114
+ self ._json .update ({key : value })
1115
+
1116
+ elif value is None and key in self ._json .keys ():
1117
+ del self ._json [key ]
1064
1118
1065
1119
def add_field (self , name : str , value : str , inline : Optional [bool ] = False ) -> None :
1066
1120
"""
@@ -1074,19 +1128,20 @@ def add_field(self, name: str, value: str, inline: Optional[bool] = False) -> No
1074
1128
:type inline?: Optional[bool]
1075
1129
"""
1076
1130
1077
- if self .fields is None :
1078
- self . fields = []
1131
+ fields = self .fields or []
1132
+ fields . append ( EmbedField ( name = name , value = value , inline = inline ))
1079
1133
1080
- self .fields .append (EmbedField (name = name , value = value , inline = inline ))
1081
- self ._json .update ({"fields" : [field ._json for field in self .fields ]})
1134
+ self .fields = fields
1135
+ # We must use "=" here to call __setattr__. Append does not call any magic, making it impossible to modify the
1136
+ # json when using it, so the object what would be sent wouldn't be modified.
1137
+ # Imo this is still better than doing a `self._json.update({"fields": [field._json for ...]})`
1082
1138
1083
1139
def clear_fields (self ) -> None :
1084
1140
"""
1085
1141
Clears all the fields of the embed
1086
1142
"""
1087
1143
1088
1144
self .fields = []
1089
- self ._json .update ({"fields" : []})
1090
1145
1091
1146
def insert_field_at (
1092
1147
self , index : int , name : str = None , value : str = None , inline : Optional [bool ] = False
@@ -1104,13 +1159,9 @@ def insert_field_at(
1104
1159
:type inline?: Optional[bool]
1105
1160
"""
1106
1161
1107
- try :
1108
- self .fields .insert (index , EmbedField (name = name , value = value , inline = inline ))
1109
-
1110
- except AttributeError :
1111
- self .fields = [EmbedField (name = name , value = value , inline = inline )]
1112
-
1113
- self ._json .update ({"fields" : [field ._json for field in self .fields ]})
1162
+ fields = self .fields or []
1163
+ fields .insert (index , EmbedField (name = name , value = value , inline = inline ))
1164
+ self .fields = fields
1114
1165
1115
1166
def set_field_at (
1116
1167
self , index : int , name : str , value : str , inline : Optional [bool ] = False
@@ -1130,7 +1181,6 @@ def set_field_at(
1130
1181
1131
1182
try :
1132
1183
self .fields [index ] = EmbedField (name = name , value = value , inline = inline )
1133
- self ._json .update ({"fields" : [field ._json for field in self .fields ]})
1134
1184
1135
1185
except AttributeError as e :
1136
1186
raise AttributeError ("No fields found in Embed" ) from e
@@ -1147,8 +1197,9 @@ def remove_field(self, index: int) -> None:
1147
1197
"""
1148
1198
1149
1199
try :
1150
- self .fields .pop (index )
1151
- self ._json .update ({"fields" : [field ._json for field in self .fields ]})
1200
+ fields = self .fields
1201
+ fields .pop (index )
1202
+ self .fields = fields
1152
1203
1153
1204
except AttributeError as e :
1154
1205
raise AttributeError ("No fields found in Embed" ) from e
@@ -1163,7 +1214,6 @@ def remove_author(self) -> None:
1163
1214
1164
1215
try :
1165
1216
del self .author
1166
- self ._json .update ({"author" : None })
1167
1217
except AttributeError :
1168
1218
pass
1169
1219
@@ -1190,7 +1240,6 @@ def set_author(
1190
1240
self .author = EmbedAuthor (
1191
1241
name = name , url = url , icon_url = icon_url , proxy_icon_url = proxy_icon_url
1192
1242
)
1193
- self ._json .update ({"author" : self .author ._json })
1194
1243
1195
1244
def set_footer (
1196
1245
self , text : str , icon_url : Optional [str ] = None , proxy_icon_url : Optional [str ] = None
@@ -1207,7 +1256,6 @@ def set_footer(
1207
1256
"""
1208
1257
1209
1258
self .footer = EmbedFooter (text = text , icon_url = icon_url , proxy_icon_url = proxy_icon_url )
1210
- self ._json .update ({"footer" : self .footer ._json })
1211
1259
1212
1260
def set_image (
1213
1261
self ,
@@ -1230,7 +1278,6 @@ def set_image(
1230
1278
"""
1231
1279
1232
1280
self .image = EmbedImageStruct (url = url , proxy_url = proxy_url , height = height , width = width )
1233
- self ._json .update ({"image" : self .image ._json })
1234
1281
1235
1282
def set_thumbnail (
1236
1283
self ,
@@ -1253,4 +1300,3 @@ def set_thumbnail(
1253
1300
"""
1254
1301
1255
1302
self .thumbnail = EmbedImageStruct (url = url , proxy_url = proxy_url , height = height , width = width )
1256
- self ._json .update ({"thumbnail" : self .thumbnail ._json })
0 commit comments