-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimedBackgroundPriceService.cs
646 lines (546 loc) · 19.2 KB
/
TimedBackgroundPriceService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
using CoinGecko.Entities.Response.Coins;
using CoinGecko.Entities.Response.Simple;
using CoinGecko.Interfaces;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Discord.Rest;
using Discord.WebSocket;
using Discord;
using System.Net;
using System.Net.Http;
using System.IO;
using System.Text.Json.Serialization;
using System.Text.Json;
using System.Diagnostics;
namespace DiscordCryptoSidebarBot
{
public class TimedBackgroundPriceService : IHostedService, IDisposable
{
private readonly ILogger<TimedBackgroundPriceService> _logger;
private readonly BotSettings _settings;
private readonly ICoinGeckoClient _client;
private DiscordRestClient _discordRestClient = null!;
private DiscordSocketClient _discordSocketClient = null!;
private EthGasService _ethGasService = null!;
private CustomApiService _customApiService = null!;
private string _coinName = null!;
private bool _firstRun = true;
private Timer _timer = null!;
private Dictionary<ulong, (ulong?, ulong?)> _guildToRoleIds;
class PriceBotActivity : IActivity
{
public string Name { get; set; }
public ActivityType Type => ActivityType.Playing;
public ActivityProperties Flags => ActivityProperties.None;
public string Details { get; set; }
}
public TimedBackgroundPriceService(ILogger<TimedBackgroundPriceService> logger,
IOptions<BotSettings> settings,
ICoinGeckoClient client,
DiscordRestClient discordRestClient,
DiscordSocketClient discordSocketClient,
EthGasService ethGasService,
CustomApiService customApiService)
{
_logger = logger;
_settings = settings.Value;
_client = client;
_discordRestClient = discordRestClient;
_discordSocketClient = discordSocketClient;
_ethGasService = ethGasService;
_customApiService = customApiService;
_guildToRoleIds = new Dictionary<ulong, (ulong?, ulong?)>();
}
public Task StartAsync(CancellationToken stoppingToken)
{
ExecuteAsync().Wait();
return Task.CompletedTask;
}
private bool InGasMode
{
get
{
return _settings.ApiId?.ToLowerInvariant() == "ethgas";
}
}
private bool InCustomApiMode
{
get
{
return !string.IsNullOrWhiteSpace(_settings.CustomEndpoint);
}
}
private bool HasCustomTicker
{
get
{
return !string.IsNullOrWhiteSpace(_settings.CustomTicker);
}
}
private bool HasCustomAvatar
{
get
{
return !string.IsNullOrWhiteSpace(_settings.CustomAvatar);
}
}
private bool HasCustomEmoji
{
get
{
return !string.IsNullOrWhiteSpace(_settings.CustomEmoji);
}
}
private bool IsClustered
{
get
{
return !string.IsNullOrEmpty(_settings.ClusterPath) && !string.IsNullOrEmpty(_settings.ClusterName);
}
}
private string? ClusterFilename
{
get
{
return IsClustered ? Path.Combine(_settings.ClusterPath!, $"{_settings.ClusterName!}.bot") : null;
}
}
private string? ClusterSimplePriceFilename
{
get
{
return IsClustered ? Path.Combine(_settings.ClusterPath!, "simpleprice.json") : null;
}
}
private string? CoinlistFilename
{
get
{
return IsClustered ? Path.Combine(_settings.ClusterPath!, "coinlist.json") : null;
}
}
private bool IsClusterLeader
{
get
{
return IsClustered && _settings.ClusterLeader.HasValue && _settings.ClusterLeader.Value;
}
}
private async Task ConfigureCluster()
{
if (IsClustered)
{
Directory.CreateDirectory(_settings.ClusterPath!);
using (var f = File.Create(ClusterFilename!))
using (var s = new StreamWriter(f))
{
await s.WriteAsync(_settings.ApiId);
}
}
}
private async Task ExecuteAsync()
{
_logger.LogInformation("TimedBackgroundPriceService running.");
await ConfigureCluster();
await _discordRestClient.LoginAsync(TokenType.Bot, _settings.BotToken);
await _discordSocketClient.LoginAsync(TokenType.Bot, _settings.BotToken);
await _discordSocketClient.StartAsync();
_discordSocketClient.Connected += DiscordSocketClientConnected;
_timer = new Timer(DoWork, null, TimeSpan.FromSeconds(_settings.Delay),
TimeSpan.FromSeconds(_settings.UpdateInterval));
}
private async Task DiscordSocketClientConnected()
{
if (InGasMode)
{
await SetLogo("ethgas.png");
return;
}
if (InCustomApiMode)
{
return;
}
if (HasCustomAvatar)
{
if (IsLocalPath(_settings.CustomAvatar))
{
await SetLogo(_settings.CustomAvatar);
}
else
{
var uri = new Uri(_settings.CustomAvatar);
await SetLogoFromUri(uri);
}
}
else
{
var coinInfo = await _client.CoinsClient.GetAllCoinDataWithId(_settings.ApiId);
await SetLogoFromCoinInfo(coinInfo);
}
var guilds = _discordSocketClient.Guilds;
foreach (var guild in guilds)
{
var gainRoleId = guild.Roles.FirstOrDefault(x => x.Name.ToLowerInvariant() == _settings.GainRoleName.ToLowerInvariant())?.Id;
var lossRoleId = guild.Roles.FirstOrDefault(x => x.Name.ToLowerInvariant() == _settings.LossRoleName.ToLowerInvariant())?.Id;
_guildToRoleIds[guild.Id] = (gainRoleId, lossRoleId);
}
}
private async Task SetLogoFromUri(Uri url)
{
var fileName = url.Segments.Last();
if (!Directory.Exists("images"))
{
Directory.CreateDirectory("images");
}
var path = Path.Combine("images", fileName);
// Download Coin Logo
using (var client = new HttpClient())
{
var response = client.GetAsync(url).GetAwaiter().GetResult();
response.EnsureSuccessStatusCode();
var ms = response.Content.ReadAsStreamAsync().GetAwaiter().GetResult();
var fs = File.Create(path);
ms.Seek(0, SeekOrigin.Begin);
ms.CopyTo(fs);
fs.Close();
ms.Close();
}
await SetLogo(path);
}
private async Task SetLogoFromCoinInfo(CoinFullDataById coinInfo)
{
await SetLogoFromUri(coinInfo.Image.Large);
}
private async Task SetLogo(string dir)
{
await _discordSocketClient.CurrentUser.ModifyAsync((p) =>
{
p.Avatar = new Optional<Image?>(new Image(dir));
});
}
private static decimal? GetValue(Price? price, string apiId, string field)
{
var dollars = string.Empty;
if (price != null)
{
var p = price.FirstOrDefault(x => x.Key == apiId).Value.FirstOrDefault(x => x.Key == field).Value;
return p;
}
return null;
}
private static decimal? PriceToDollarValue(Price? price, string apiId)
{
return GetValue(price, apiId, "usd");
}
private static decimal? PriceToChangeLast24Hr(Price? price, string apiId)
{
return GetValue(price, apiId, "usd_24h_change");
}
private static string GetCoinNameFromApiId(IReadOnlyList<CoinList> coinList, string apiId)
{
if (string.IsNullOrEmpty(apiId))
{
throw new ArgumentNullException(nameof(apiId));
}
if (coinList == null || coinList.Count == 0)
return "!UNKNOWN!";
var coin = coinList.FirstOrDefault(x => x.Id == apiId)!.Symbol.ToUpperInvariant();
return coin;
}
private static string RenderDirection(decimal? value)
{
return value switch
{
>= 80 => "🌕",
>= 50 => "🚀",
>= 35 => "💰",
>= 30 => "🔥",
>= 25 => "🤑",
>= 20 => "🏌",
>= 15 => "😝",
>= 10 => "😁",
>= 7 => "😃",
>= 5 => "🙂",
> 0 => "📈",
<= -80 => "☠️",
<= -50 => "😀🔫",
<= -40 => "🚽",
<= -35 => "💩",
<= -30 => "😱",
<= -25 => "💸",
<= -20 => "🤢",
<= -15 => "🤬",
<= -10 => "🥺",
<= -7 => "😤",
<= -5 => "😅",
< 0 => "📉",
0 => "😐",
_ => "😐"
};
}
private static bool HasDecimal(decimal? value)
{
var val = value.ToString()!;
if (val.Contains("."))
{
var dec = val.Split('.');
if (dec[0] == "0" || dec[0] == "00")
{
return false;
}
return true;
}
return false;
}
private static string RenderCurrency(decimal? value)
{
bool hasDecimal = HasDecimal(value);
if (value < 1)
{
return string.Format("{0:C4}", value);
}
if (hasDecimal && value >= 1000m)
{
return string.Format("{0:C0}", value);
}
if (hasDecimal)
{
return string.Format("{0:C}", value);
}
return string.Format("{0:C0}", value);
}
private static string RenderPercent(decimal? value)
{
return string.Format("{0:0.00}%", value);
}
private void DoWork(object? state)
{
Process().GetAwaiter().GetResult();
}
private async Task<IReadOnlyList<CoinList>> GetCoinListAsync()
{
if (IsClusterLeader)
{
var coinlist = await _client.CoinsClient.GetCoinList();
using (var f = File.Create(CoinlistFilename!))
using (var s = new StreamWriter(f))
{
await JsonSerializer.SerializeAsync(s.BaseStream, coinlist);
}
return coinlist;
}
else
{
using(var f = File.OpenRead(CoinlistFilename!))
using(var s = new StreamReader(f))
{
var coinlist = (await JsonSerializer.DeserializeAsync<IReadOnlyList<CoinList>>(s.BaseStream))!;
return coinlist;
}
}
}
private async Task<Price?> GetSimplePrice()
{
if (IsClusterLeader)
{
var price = await _client.SimpleClient.GetSimplePrice(ClusterApiIds, new string[] { "usd" }, false, false, true, false);
using (var f = File.Create(ClusterSimplePriceFilename))
using (var s = new StreamWriter(f))
{
await JsonSerializer.SerializeAsync(s.BaseStream, price);
return price;
}
}
else
{
using(var f = File.OpenRead(ClusterSimplePriceFilename))
using(var s = new StreamReader(f))
{
var price = await JsonSerializer.DeserializeAsync<Price>(s.BaseStream);
return price;
}
}
}
private string[] ClusterApiIds
{
get
{
var files = Directory.GetFiles(_settings.ClusterPath!, "*.bot", SearchOption.AllDirectories);
var apiIds = new List<string>();
foreach(var file in files)
{
var coinId = File.ReadAllText(file);
apiIds.Add(coinId);
}
return apiIds.ToArray();
}
}
private async Task Process()
{
try
{
string nickname = string.Empty;
string playing = string.Empty;
if (InGasMode)
{
var gas = await _ethGasService.GetGas();
nickname = $"⚡{gas.Rapid}🏃{gas.Fast}";
playing = $"🚶{gas.Standard}🐢{gas.Slow}";
await UpdateDiscordInfo(nickname, playing, null);
return;
}
if (_firstRun)
{
// Cluster should grab coinlist and cache
var coinlist = await GetCoinListAsync();
if (!HasCustomTicker)
{
_coinName = GetCoinNameFromApiId(coinlist, _settings.ApiId);
}
else
{
_coinName = _settings.CustomTicker!;
}
_firstRun = false;
}
decimal? dollarValue = null;
decimal? percentChange = null;
if (!InCustomApiMode)
{
var price = await GetSimplePrice()!;
dollarValue = PriceToDollarValue(price, _settings.ApiId);
percentChange = PriceToChangeLast24Hr(price, _settings.ApiId);
}
else
{
dollarValue = await _customApiService.GetPrice();
}
if (!HasCustomEmoji)
{
nickname = $"{_coinName} {RenderCurrency(dollarValue)} {RenderDirection(percentChange)}";
}
else
{
nickname = $"{_coinName} {RenderCurrency(dollarValue)} {_settings.CustomEmoji}";
}
if (percentChange != null)
{
playing = $"$ 24h: {RenderPercent(percentChange)}";
}
await UpdateDiscordInfo(nickname, playing, percentChange);
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Error occured while attempting to update, skipping this pass...");
}
}
private async Task UpdateDiscordInfo(string nickname, string playing, decimal? percentChange)
{
var guilds = await _discordRestClient.GetGuildsAsync();
var activity = new PriceBotActivity();
activity.Name = playing;
activity.Details = playing;
_logger.LogInformation($"{nickname} - {playing}");
Console.WriteLine($"{nickname} - {playing}");
try
{
await _discordSocketClient.SetActivityAsync(activity);
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to set Discord Activity");
Console.Error.WriteLine($"Failed to set Discord Activity, {ex}");
}
foreach (var guild in guilds)
{
var user = await guild.GetCurrentUserAsync();
try
{
await AssignGuildRoles(percentChange, user, guild.Id);
}
catch(Exception ex)
{
_logger.LogError(ex, "Failed to set Discord Guild role in {guildid}", guild.Id);
Console.Error.WriteLine($"Failed to set Guild role in {guild.Id}, {ex}");
}
try
{
await user.ModifyAsync(x =>
{
x.Nickname = nickname;
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to set Discord Nickname");
Console.Error.WriteLine($"Failed to set Discord Nickname, {ex}");
}
}
}
private async Task AssignGuildRoles(decimal? percentChange, RestGuildUser user, ulong guildId)
{
if (percentChange == null)
return;
if (!_guildToRoleIds.ContainsKey(guildId))
return;
var roles = _guildToRoleIds[guildId];
var gainRoleId = roles.Item1;
var lossRoleId = roles.Item2;
if (gainRoleId != null && percentChange.HasValue)
{
if (percentChange.Value >= 0)
{
if (!user.RoleIds.Contains(gainRoleId.Value))
{
await user.AddRoleAsync(gainRoleId.Value);
}
}
else
{
if (user.RoleIds.Contains(gainRoleId.Value))
{
await user.RemoveRoleAsync(gainRoleId.Value);
}
}
}
if (lossRoleId != null && percentChange.HasValue)
{
if (percentChange.Value < 0)
{
if (!user.RoleIds.Contains(lossRoleId.Value))
{
await user.AddRoleAsync(lossRoleId.Value);
}
}
else
{
if (user.RoleIds.Contains(lossRoleId.Value))
{
await user.RemoveRoleAsync(lossRoleId.Value);
}
}
}
}
private static bool IsLocalPath(string p)
{
return new Uri(p).IsFile;
}
public Task StopAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("TimedBackgroundPriceService is stopping.");
_timer?.Change(Timeout.Infinite, 0);
return Task.CompletedTask;
}
public void Dispose()
{
_timer?.Dispose();
}
}
}