1
+ using Newtonsoft . Json ;
2
+ using CnD_Sound . Config ;
3
+ using System . Text ;
4
+ using System . Drawing ;
5
+
6
+ namespace CnD_Sound ;
7
+
8
+ public class CnD_SoundJson
9
+ {
10
+ private static readonly HttpClient _httpClient = new HttpClient ( ) ;
11
+ private static readonly HttpClient httpClient = new HttpClient ( ) ;
12
+ private class PersonData
13
+ {
14
+ public int Id { get ; set ; }
15
+ public bool BoolValue { get ; set ; }
16
+ public DateTime Date { get ; set ; }
17
+ }
18
+ public static async Task SendToDiscordWebhookNormal ( string webhookUrl , string message )
19
+ {
20
+ try
21
+ {
22
+ var payload = new { content = message } ;
23
+ var jsonPayload = Newtonsoft . Json . JsonConvert . SerializeObject ( payload ) ;
24
+ var content = new StringContent ( jsonPayload , Encoding . UTF8 , "application/json" ) ;
25
+
26
+ var response = await _httpClient . PostAsync ( webhookUrl , content ) . ConfigureAwait ( false ) ;
27
+
28
+
29
+ }
30
+ catch
31
+ {
32
+ }
33
+ }
34
+
35
+ public static async Task SendToDiscordWebhookNameLink ( string webhookUrl , string message , string steamUserId , string STEAMNAME )
36
+ {
37
+ try
38
+ {
39
+ string profileLink = GetSteamProfileLink ( steamUserId ) ;
40
+ int colorss = int . Parse ( Configs . GetConfigData ( ) . Log_DiscordSideColor , System . Globalization . NumberStyles . HexNumber ) ;
41
+ Color color = Color . FromArgb ( colorss >> 16 , ( colorss >> 8 ) & 0xFF , colorss & 0xFF ) ;
42
+ using ( var httpClient = new HttpClient ( ) )
43
+ {
44
+ var embed = new
45
+ {
46
+ type = "rich" ,
47
+ title = STEAMNAME ,
48
+ url = profileLink ,
49
+ description = message ,
50
+ color = color . ToArgb ( ) & 0xFFFFFF
51
+ } ;
52
+
53
+ var payload = new
54
+ {
55
+ embeds = new [ ] { embed }
56
+ } ;
57
+
58
+ var jsonPayload = Newtonsoft . Json . JsonConvert . SerializeObject ( payload ) ;
59
+ var content = new StringContent ( jsonPayload , Encoding . UTF8 , "application/json" ) ;
60
+ var response = await _httpClient . PostAsync ( webhookUrl , content ) . ConfigureAwait ( false ) ;
61
+
62
+ }
63
+ }
64
+ catch
65
+ {
66
+ }
67
+ }
68
+ public static async Task SendToDiscordWebhookNameLinkWithPicture ( string webhookUrl , string message , string steamUserId , string STEAMNAME )
69
+ {
70
+ try
71
+ {
72
+ string profileLink = GetSteamProfileLink ( steamUserId ) ;
73
+ string profilePictureUrl = await GetProfilePictureAsync ( steamUserId , Configs . GetConfigData ( ) . Log_DiscordUsersWithNoAvatarImage ) ;
74
+ int colorss = int . Parse ( Configs . GetConfigData ( ) . Log_DiscordSideColor , System . Globalization . NumberStyles . HexNumber ) ;
75
+ Color color = Color . FromArgb ( colorss >> 16 , ( colorss >> 8 ) & 0xFF , colorss & 0xFF ) ;
76
+ using ( var httpClient = new HttpClient ( ) )
77
+ {
78
+ var embed = new
79
+ {
80
+ type = "rich" ,
81
+ description = message ,
82
+ color = color . ToArgb ( ) & 0xFFFFFF ,
83
+ author = new
84
+ {
85
+ name = STEAMNAME ,
86
+ url = profileLink ,
87
+ icon_url = profilePictureUrl
88
+ }
89
+ } ;
90
+
91
+ var payload = new
92
+ {
93
+ embeds = new [ ] { embed }
94
+ } ;
95
+
96
+ var jsonPayload = Newtonsoft . Json . JsonConvert . SerializeObject ( payload ) ;
97
+ var content = new StringContent ( jsonPayload , Encoding . UTF8 , "application/json" ) ;
98
+ var response = await _httpClient . PostAsync ( webhookUrl , content ) . ConfigureAwait ( false ) ;
99
+ }
100
+ }
101
+ catch
102
+ {
103
+
104
+ }
105
+ }
106
+ public static async Task < string > GetProfilePictureAsync ( string steamId64 , string defaultImage )
107
+ {
108
+ try
109
+ {
110
+ string apiUrl = $ "https://steamcommunity.com/profiles/{ steamId64 } /?xml=1";
111
+
112
+ HttpResponseMessage response = await httpClient . GetAsync ( apiUrl ) ;
113
+
114
+ if ( response . IsSuccessStatusCode )
115
+ {
116
+ string xmlResponse = await response . Content . ReadAsStringAsync ( ) ;
117
+ int startIndex = xmlResponse . IndexOf ( "<avatarFull><![CDATA[" ) + "<avatarFull><![CDATA[" . Length ;
118
+ int endIndex = xmlResponse . IndexOf ( "]]></avatarFull>" , startIndex ) ;
119
+
120
+ if ( endIndex >= 0 )
121
+ {
122
+ string profilePictureUrl = xmlResponse . Substring ( startIndex , endIndex - startIndex ) ;
123
+ return profilePictureUrl ;
124
+ }
125
+ else
126
+ {
127
+ return defaultImage ;
128
+ }
129
+ }
130
+ else
131
+ {
132
+ return null ! ;
133
+ }
134
+ }
135
+ catch
136
+ {
137
+ return null ! ;
138
+ }
139
+ }
140
+ public static string GetSteamProfileLink ( string userId )
141
+ {
142
+ return $ "https://steamcommunity.com/profiles/{ userId } ";
143
+ }
144
+
145
+ public static void SaveToJsonFile ( int id , bool boolValue , DateTime date )
146
+ {
147
+ string cookiesFilePath = Configs . Shared . CookiesFolderPath ! ;
148
+ string Fpath = Path . Combine ( cookiesFilePath , "../../plugins/CnD_Sound/Cookies/" ) ;
149
+ string Fpathc = Path . Combine ( cookiesFilePath , "../../plugins/CnD_Sound/Cookies/CnD_Sound_Cookies.json" ) ;
150
+ try
151
+ {
152
+ if ( ! Directory . Exists ( Fpath ) )
153
+ {
154
+ Directory . CreateDirectory ( Fpath ) ;
155
+ }
156
+
157
+ if ( ! File . Exists ( Fpathc ) )
158
+ {
159
+ File . WriteAllText ( Fpathc , "[]" ) ;
160
+ }
161
+
162
+ List < PersonData > allPersonsData ;
163
+ string jsonData = File . ReadAllText ( Fpathc ) ;
164
+ allPersonsData = JsonConvert . DeserializeObject < List < PersonData > > ( jsonData ) ?? new List < PersonData > ( ) ;
165
+
166
+ PersonData existingPerson = allPersonsData . Find ( p => p . Id == id ) ! ;
167
+
168
+ if ( existingPerson != null )
169
+ {
170
+ existingPerson . BoolValue = boolValue ;
171
+ existingPerson . Date = date ;
172
+ }
173
+ else
174
+ {
175
+ PersonData newPerson = new PersonData { Id = id , BoolValue = boolValue , Date = date } ;
176
+ allPersonsData . Add ( newPerson ) ;
177
+ }
178
+ allPersonsData . RemoveAll ( p => ( DateTime . Now - p . Date ) . TotalDays > Configs . GetConfigData ( ) . RemovePlayerCookieOlderThanXDays ) ;
179
+
180
+ string updatedJsonData = JsonConvert . SerializeObject ( allPersonsData , Formatting . Indented ) ;
181
+ try
182
+ {
183
+ File . WriteAllText ( Fpathc , updatedJsonData ) ;
184
+ } catch
185
+ {
186
+ }
187
+ } catch
188
+ {
189
+ }
190
+ }
191
+
192
+ public static bool RetrieveBoolValueById ( int targetId )
193
+ {
194
+ string cookiesFilePath = Configs . Shared . CookiesFolderPath ! ;
195
+ string Fpath = Path . Combine ( cookiesFilePath , "../../plugins/CnD_Sound/Cookies/" ) ;
196
+ string Fpathc = Path . Combine ( cookiesFilePath , "../../plugins/CnD_Sound/Cookies/CnD_Sound_Cookies.json" ) ;
197
+ try
198
+ {
199
+ if ( File . Exists ( Fpathc ) )
200
+ {
201
+ string jsonData = File . ReadAllText ( Fpathc ) ;
202
+ List < PersonData > allPersonsData = JsonConvert . DeserializeObject < List < PersonData > > ( jsonData ) ?? new List < PersonData > ( ) ;
203
+
204
+ PersonData targetPerson = allPersonsData . Find ( p => p . Id == targetId ) ! ;
205
+
206
+ if ( targetPerson != null )
207
+ {
208
+ if ( DateTime . Now - targetPerson . Date <= TimeSpan . FromDays ( Configs . GetConfigData ( ) . RemovePlayerCookieOlderThanXDays ) )
209
+ {
210
+ return targetPerson . BoolValue ;
211
+ }
212
+ else
213
+ {
214
+ allPersonsData . Remove ( targetPerson ) ;
215
+ string updatedJsonData = JsonConvert . SerializeObject ( allPersonsData , Formatting . Indented ) ;
216
+ try
217
+ {
218
+ File . WriteAllText ( Fpathc , updatedJsonData ) ;
219
+ } catch
220
+ {
221
+ }
222
+ }
223
+ }
224
+ }
225
+ return false ;
226
+ } catch
227
+ {
228
+ return false ;
229
+ }
230
+ }
231
+ }
0 commit comments