forked from ValkyrjaProject/Valkyrja.core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBotwinder-client.cs
1052 lines (895 loc) · 32.7 KB
/
Botwinder-client.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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using Botwinder.entities;
using Discord;
using Discord.WebSocket;
using guid = System.UInt64;
namespace Botwinder.core
{
public partial class BotwinderClient : IBotwinderClient, IDisposable
{
public readonly DbConfig DbConfig;
private GlobalContext GlobalDb;
private ServerContext ServerDb;
public GlobalConfig GlobalConfig{ get; set; }
public List<guid> SupportTeam{ get; set; }
public Shard CurrentShard{ get; set; }
public DiscordSocketClient DiscordClient;
public Events Events;
public string DbConnectionString;
public DateTime TimeStarted{ get; private set; }
private DateTime TimeConnected = DateTime.MaxValue;
private bool IsInitialized = false;
public bool IsConnected{
get => this.DiscordClient.LoginState == LoginState.LoggedIn &&
this.DiscordClient.ConnectionState == ConnectionState.Connected &&
this._Connected;
set => this._Connected = value;
}
private bool _Connected = false;
private CancellationTokenSource MainUpdateCancel;
private Task MainUpdateTask = null;
public readonly List<IModule> Modules = new List<IModule>();
private const string GameStatusConnecting = "Connecting...";
private const string GameStatusUrl = "at http://botwinder.info";
private readonly Regex RegexCommandParams = new Regex("\"[^\"]+\"|\\S+", RegexOptions.Compiled);
private readonly Regex RegexEveryone = new Regex("(@everyone)|(@here)", RegexOptions.Compiled);
public readonly ConcurrentDictionary<guid, Server> Servers = new ConcurrentDictionary<guid, Server>();
public readonly Dictionary<string, Command> Commands = new Dictionary<string, Command>();
public Dictionary<guid, Subscriber> Subscribers = new Dictionary<guid, Subscriber>();
public Dictionary<guid, PartneredServer> PartneredServers = new Dictionary<guid, PartneredServer>();
public List<Operation> CurrentOperations{ get; set; } = new List<Operation>();
public Object OperationsLock{ get; set; } = new Object();
public Object DbLock{ get; set; } = new Object();
private bool ValidSubscribers = false;
private readonly List<guid> LeaveNotifiedOwners = new List<guid>();
private DateTime LastMessageAverageTime = DateTime.UtcNow;
private int MessagesThisMinute = 0;
public ConcurrentDictionary<guid, List<guid>> ClearedMessageIDs = new ConcurrentDictionary<guid, List<guid>>();
public List<guid> AntispamMessageIDs = new List<guid>();
public BotwinderClient(int shardIdOverride = -1)
{
this.TimeStarted = DateTime.UtcNow;
this.DbConfig = DbConfig.Load();
this.DbConnectionString = this.DbConfig.GetDbConnectionString();
this.GlobalDb = GlobalContext.Create(this.DbConnectionString);
this.ServerDb = ServerContext.Create(this.DbConnectionString);
if( ++shardIdOverride > 0 )
this.DbConfig.ForceShardId = shardIdOverride; //db shards count from one
}
public void Dispose()
{
Console.WriteLine("Disposing of the client.");
if( this.CurrentShard != null )
{
GlobalContext dbContext = GlobalContext.Create(this.DbConnectionString);
Shard shard = dbContext.Shards.FirstOrDefault(s => s.Id == this.CurrentShard.Id);
if( shard != null )
{
shard.IsTaken = false;
shard.IsConnecting = false;
dbContext.SaveChanges();
}
dbContext.Dispose();
}
if( this.GlobalDb != null )
this.GlobalDb.Dispose();
this.GlobalDb = null;
if( this.ServerDb != null )
this.ServerDb.Dispose();
this.ServerDb = null;
//todo
Console.WriteLine("Disposed of the client.");
}
public async Task Connect()
{
LoadConfig();
lock(this.DbLock)
{
if( (this.DbConfig.ForceShardId == 0 && this.GlobalConfig.TotalShards > this.GlobalDb.Shards.Count()) ||
this.DbConfig.ForceShardId > this.GlobalDb.Shards.Count() + 1 ) //they start at 1...
{
Console.WriteLine("BotwinderClient: TotalShards (or forceId) exceeds the Shards count!!!");
Dispose();
Environment.Exit(1);
}
}
//Find a shard for grabs.
if( this.DbConfig.ForceShardId > 0 )
this.CurrentShard = this.GlobalDb.Shards.FirstOrDefault(s => s.Id == this.DbConfig.ForceShardId);
else
{
Shard GetShard()
{
lock(this.DbLock)
this.CurrentShard = this.GlobalDb.Shards.FirstOrDefault(s => s.IsTaken == false);
return this.CurrentShard;
}
Console.WriteLine("BotwinderClient: Waiting for a shard...");
while( GetShard() == null )
{
await Task.Delay(Utils.Random.Next(5000, 10000));
}
this.CurrentShard.IsTaken = true;
lock(this.DbLock)
this.GlobalDb.SaveChanges();
}
Console.WriteLine($"BotwinderClient: Shard {this.CurrentShard.Id - 1} taken.");
this.CurrentShard.ResetStats(this.TimeStarted);
DiscordSocketConfig config = new DiscordSocketConfig();
config.ShardId = (int) this.CurrentShard.Id - 1; //Shard's start at one in the database.
config.TotalShards = (int) this.GlobalConfig.TotalShards;
config.LogLevel = this.GlobalConfig.LogDebug ? LogSeverity.Debug : LogSeverity.Warning;
config.DefaultRetryMode = RetryMode.Retry502 & RetryMode.RetryRatelimit & RetryMode.RetryTimeouts;
config.AlwaysDownloadUsers = this.DbConfig.DownloadUsers;
config.LargeThreshold = 100;
config.HandlerTimeout = null;
config.MessageCacheSize = 100;
config.ConnectionTimeout = 300000;
this.DiscordClient = new DiscordSocketClient(config);
/*if( this.GlobalConfig.LogDebug )
{
this.DiscordClient.Log += message => {
Console.WriteLine($"[${message.Severity}] ${message.Message}\n Source: ${message.Source}");
return Task.CompletedTask;
};
}*/
this.DiscordClient.Connecting += OnConnecting;
this.DiscordClient.Connected += OnConnected;
this.DiscordClient.Ready += OnReady;
this.DiscordClient.Disconnected += OnDisconnected;
this.Events = new Events(this.DiscordClient);
this.Events.MessageReceived += OnMessageReceived;
this.Events.MessageUpdated += OnMessageUpdated;
this.Events.LogEntryAdded += Log;
this.Events.Exception += Log;
this.Events.Connected += async () => await this.DiscordClient.SetGameAsync(GameStatusUrl);
this.Events.Initialize += InitCommands;
this.Events.Initialize += InitModules;
this.Events.GuildAvailable += OnGuildAvailable;
this.Events.JoinedGuild += OnGuildJoined;
this.Events.LeftGuild += OnGuildLeft;
this.Events.GuildUpdated += OnGuildUpdated;
this.Events.UserJoined += OnUserJoined;
this.Events.UserUpdated += OnUserUpdated;
this.Events.GuildMemberUpdated += OnGuildMemberUpdated;
this.Events.GuildMembersDownloaded += OnGuildMembersDownloaded;
await this.DiscordClient.LoginAsync(TokenType.Bot, this.GlobalConfig.DiscordToken);
await this.DiscordClient.StartAsync();
}
private void LoadConfig()
{
Console.WriteLine("BotwinderClient: Loading configuration...");
lock(this.DbLock)
{
bool save = false;
if( !this.GlobalDb.GlobalConfigs.Any() )
{
this.GlobalDb.GlobalConfigs.Add(new GlobalConfig());
save = true;
}
if( !this.GlobalDb.Shards.Any() )
{
this.GlobalDb.Shards.Add(new Shard(){Id = 0});
save = true;
}
if( save )
this.GlobalDb.SaveChanges();
this.GlobalConfig = this.GlobalDb.GlobalConfigs.FirstOrDefault(c => c.ConfigName == this.DbConfig.ConfigName);
if( this.GlobalConfig == null )
{
this.GlobalConfig = new GlobalConfig(){ConfigName = this.DbConfig.ConfigName};
this.GlobalDb.GlobalConfigs.Add(this.GlobalConfig);
this.GlobalDb.SaveChanges();
Console.WriteLine("BotwinderClient: Configuration created.");
Environment.Exit(0);
}
this.SupportTeam = this.GlobalDb.SupportTeam.Select(u => u.UserId).ToList();
}
Console.WriteLine("BotwinderClient: Configuration loaded.");
}
//Events
private async Task OnConnecting()
{
//Some other node is already connecting, wait.
if( this.DbConfig.UseShardLock )
{
bool IsAnyShardConnecting()
{
lock(this.DbLock)
return this.GlobalDb.Shards.Any(s => s.IsConnecting);
}
bool awaited = false;
while( IsAnyShardConnecting() )
{
if( !awaited )
Console.WriteLine("BotwinderClient: Waiting for other shards to connect...");
awaited = true;
await Task.Delay(Utils.Random.Next(5000, 10000));
}
this.CurrentShard.IsConnecting = true;
lock(this.DbLock)
this.GlobalDb.SaveChanges();
if( awaited )
await Task.Delay(5000); //Ensure sufficient delay between connecting shards.
}
Console.WriteLine("BotwinderClient: Connecting...");
}
private async Task OnConnected()
{
Console.WriteLine("BotwinderClient: Connected.");
try
{
if( this.DbConfig.UseShardLock )
{
this.CurrentShard.IsConnecting = false;
lock(this.DbLock)
this.GlobalDb.SaveChanges();
}
this.TimeConnected = DateTime.Now;
await this.DiscordClient.SetGameAsync(GameStatusConnecting);
}
catch(Exception e)
{
await LogException(e, "--OnConnected");
}
}
private Task OnReady()
{
if( this.GlobalConfig.LogDebug )
Console.WriteLine("BotwinderClient: Ready.");
if( this.MainUpdateTask == null )
{
this.MainUpdateCancel = new CancellationTokenSource();
this.MainUpdateTask = Task.Factory.StartNew(MainUpdate, this.MainUpdateCancel.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
this.MainUpdateTask.Start();
}
return Task.CompletedTask;
}
private async Task OnDisconnected(Exception exception)
{
Console.WriteLine("BotwinderClient: Disconnected.");
this.IsConnected = false;
this.CurrentShard.Disconnects++;
if( exception.Message != "WebSocket connection was closed" ) //hack to not spam my logs
await LogException(exception, "--D.NET Client Disconnected");
try
{
if( this.Events.Disconnected != null )
await this.Events.Disconnected(exception);
}
catch(Exception e)
{
await LogException(e, "--Events.Disconnected");
}
Dispose();
Console.WriteLine("Shutting down.");
Environment.Exit(0); //HACK - The library often reconnects in really shitty way and no longer works
}
// Message events
private async Task OnMessageReceived(SocketMessage message)
{
if( !this.IsConnected )
return;
try
{
if( this.GlobalConfig.LogDebug )
Console.WriteLine("BotwinderClient: MessageReceived on thread " + Thread.CurrentThread.ManagedThreadId);
this.CurrentShard.MessagesTotal++;
this.MessagesThisMinute++;
if( !(message.Channel is SocketTextChannel channel) )
{
await LogMessage(LogType.Pm, null, message);
return;
}
Server server;
if( !this.Servers.ContainsKey(channel.Guild.Id) || (server = this.Servers[channel.Guild.Id]) == null )
return;
if( server.Config.IgnoreBots && message.Author.IsBot || server.Config.IgnoreEveryone && this.RegexEveryone.IsMatch(message.Content) )
return;
bool commandExecuted = false;
string prefix;
if( (!string.IsNullOrWhiteSpace(server.Config.CommandPrefix) && message.Content.StartsWith(prefix = server.Config.CommandPrefix)) ||
(!string.IsNullOrWhiteSpace(server.Config.CommandPrefixAlt) && message.Content.StartsWith(prefix = server.Config.CommandPrefixAlt)) )
commandExecuted = await HandleCommand(server, channel, message, prefix);
if( !commandExecuted && message.MentionedUsers.Any(u => u.Id == this.DiscordClient.CurrentUser.Id) )
await HandleMentionResponse(server, channel, message);
}
catch(Exception exception)
{
await LogException(exception, "--OnMessageReceived");
}
}
private async Task OnMessageUpdated(SocketMessage originalMessage, SocketMessage updatedMessage, ISocketMessageChannel iChannel)
{
if( !this.IsConnected || originalMessage.Content == updatedMessage.Content )
return;
try
{
Server server;
if( !(iChannel is SocketTextChannel channel) || updatedMessage?.Author == null || !this.Servers.ContainsKey(channel.Guild.Id) || (server = this.Servers[channel.Guild.Id]) == null || server.Config == null)
return;
if( server.Config.IgnoreBots && updatedMessage.Author.IsBot || server.Config.IgnoreEveryone && this.RegexEveryone.IsMatch(updatedMessage.Content) )
return;
bool commandExecuted = false;
if( server.Config.ExecuteOnEdit )
{
string prefix;
if( (!string.IsNullOrWhiteSpace(server.Config.CommandPrefix) && updatedMessage.Content.StartsWith(prefix = server.Config.CommandPrefix)) ||
(!string.IsNullOrWhiteSpace(server.Config.CommandPrefixAlt) && updatedMessage.Content.StartsWith(prefix = server.Config.CommandPrefixAlt)) )
commandExecuted = await HandleCommand(server, channel, updatedMessage, prefix);
}
}
catch(Exception exception)
{
await LogException(exception, "--OnMessageUpdated");
}
}
private Task Log(ExceptionEntry exceptionEntry)
{
try
{
GlobalContext dbContext = GlobalContext.Create(this.DbConnectionString);
dbContext.Exceptions.Add(exceptionEntry);
dbContext.SaveChanges();
dbContext.Dispose();
}
catch(Exception exception)
{
Console.WriteLine(exception.Message);
Console.WriteLine(exception.StackTrace);
Console.WriteLine($"--Failed to log exceptionEntry: {exceptionEntry.Message}");
Console.WriteLine($"--Failed to log exceptionEntry: {exceptionEntry.Data} | ServerId:{exceptionEntry.ServerId}");
if( exception.InnerException != null && exception.Message != exception.InnerException.Message )
{
Console.WriteLine(exception.InnerException.Message);
Console.WriteLine(exception.InnerException.StackTrace);
Console.WriteLine($"--Failed to log exceptionEntry: {exceptionEntry.Message}");
Console.WriteLine($"--Failed to log exceptionEntry: {exceptionEntry.Data} | ServerId:{exceptionEntry.ServerId}");
}
}
return Task.CompletedTask;
}
private Task Log(LogEntry logEntry)
{
GlobalContext dbContext = GlobalContext.Create(this.DbConnectionString);
dbContext.Log.Add(logEntry);
dbContext.SaveChanges();
dbContext.Dispose();
return Task.CompletedTask;
}
//Update
private async Task MainUpdate()
{
if( this.GlobalConfig.LogDebug )
Console.WriteLine("BotwinderClient: MainUpdate started.");
while( !this.MainUpdateCancel.IsCancellationRequested )
{
if( this.GlobalConfig.LogDebug )
Console.WriteLine("BotwinderClient: MainUpdate loop triggered at: " + Utils.GetTimestamp(DateTime.UtcNow));
DateTime frameTime = DateTime.UtcNow;
if( !this.IsInitialized )
{
if( this.GlobalConfig.LogDebug )
Console.WriteLine("BotwinderClient: Initialized.");
try
{
this.IsInitialized = true;
await this.Events.Initialize();
}
catch(Exception exception)
{
await LogException(exception, "--Events.Initialize");
}
}
if( this.DiscordClient.ConnectionState != ConnectionState.Connected ||
this.DiscordClient.LoginState != LoginState.LoggedIn ||
DateTime.Now - this.TimeConnected < TimeSpan.FromSeconds(this.GlobalConfig.InitialUpdateDelay) )
{
await Task.Delay(10000);
continue;
}
if( !this.IsConnected )
{
try
{
this.IsConnected = true;
await this.Events.Connected();
}
catch(Exception exception)
{
await LogException(exception, "--Events.Connected");
}
continue; //Don't run update in the same loop as init.
}
try
{
if( this.GlobalConfig.LogDebug )
Console.WriteLine("BotwinderClient: Update loop triggered at: " + Utils.GetTimestamp(DateTime.UtcNow));
await Update();
}
catch(Exception exception)
{
await LogException(exception, "--Update");
}
TimeSpan deltaTime = DateTime.UtcNow - frameTime;
if( this.GlobalConfig.LogDebug )
Console.WriteLine($"BotwinderClient: MainUpdate loop took: {deltaTime.TotalMilliseconds} ms");
await Task.Delay(TimeSpan.FromMilliseconds(Math.Max(this.GlobalConfig.TotalShards * 1000, (TimeSpan.FromSeconds(1f / this.GlobalConfig.TargetFps) - deltaTime).TotalMilliseconds)));
}
}
private async Task Update()
{
if( this.GlobalConfig.LogDebug )
Console.WriteLine("BotwinderClient: UpdateSubscriptions loop triggered at: " + Utils.GetTimestamp(DateTime.UtcNow));
await UpdateSubscriptions();
if( this.GlobalConfig.EnforceRequirements && this.ValidSubscribers )
{
if( this.GlobalConfig.LogDebug )
Console.WriteLine("BotwinderClient: BailBadServers loop triggered at: " + Utils.GetTimestamp(DateTime.UtcNow));
await BailBadServers();
}
if( this.GlobalConfig.LogDebug )
Console.WriteLine("BotwinderClient: UpdateShardStats loop triggered at: " + Utils.GetTimestamp(DateTime.UtcNow));
UpdateShardStats();
if( this.GlobalConfig.LogDebug )
Console.WriteLine("BotwinderClient: UpdateServerStats loop triggered at: " + Utils.GetTimestamp(DateTime.UtcNow));
await UpdateServerStats();
if( this.GlobalConfig.LogDebug )
Console.WriteLine("BotwinderClient: UpdateServerConfigs loop triggered at: " + Utils.GetTimestamp(DateTime.UtcNow));
await UpdateServerConfigs();
if( this.GlobalConfig.LogDebug )
Console.WriteLine("BotwinderClient: UpdateModules loop triggered at: " + Utils.GetTimestamp(DateTime.UtcNow));
await UpdateModules();
lock(this.DbLock)
{
if( this.GlobalConfig.LogDebug )
Console.WriteLine("BotwinderClient: SaveDatabase loop triggered at: " + Utils.GetTimestamp(DateTime.UtcNow));
this.GlobalDb.SaveChanges();
this.ServerDb.SaveChanges();
}
}
private void UpdateShardStats()
{
GlobalContext dbContext = GlobalContext.Create(this.DbConnectionString);
this.CurrentShard = dbContext.Shards.FirstOrDefault(s => s.Id == this.CurrentShard.Id);
if( DateTime.UtcNow - this.LastMessageAverageTime > TimeSpan.FromMinutes(1) )
{
this.CurrentShard.MessagesPerMinute = this.MessagesThisMinute;
this.MessagesThisMinute = 0;
this.LastMessageAverageTime = DateTime.UtcNow;
}
this.CurrentShard.TimeStarted = this.TimeStarted;
this.CurrentShard.OperationsActive = this.CurrentOperations.Count;
this.CurrentShard.ThreadsActive = Process.GetCurrentProcess().Threads.Count;
this.CurrentShard.MemoryUsed = GC.GetTotalMemory(false) / 1000000;
this.CurrentShard.ServerCount = this.Servers.Count;
this.CurrentShard.UserCount = this.DiscordClient.Guilds.Sum(s => s.MemberCount);
dbContext.SaveChanges();
dbContext.Dispose();
}
private async Task UpdateServerStats()
{
foreach( KeyValuePair<guid, Server> pair in this.Servers )
{
ServerStats stats = null;
lock(this.DbLock)
if( (stats = this.ServerDb.ServerStats.FirstOrDefault(s => s.ServerId == pair.Key)) == null &&
(stats = this.ServerDb.ServerStats.Local.FirstOrDefault(s => s.ServerId == pair.Key)) == null)
{
stats = new ServerStats();
stats.ServerId = pair.Value.Id;
this.ServerDb.ServerStats.Add(stats);
}
DateTime joinedAt = DateTime.UtcNow;
if( pair.Value.Guild.CurrentUser?.JoinedAt != null )
joinedAt = pair.Value.Guild.CurrentUser.JoinedAt.Value.UtcDateTime;
//Although D.NET lists this as nullable, D.API always provides the value. It is safe to assume that it's always there.
if( stats.JoinedTimeFirst == DateTime.MaxValue ) //This is the first time that we joined the server.
{
stats.JoinedTimeFirst = joinedAt;
stats.JoinedCount = 1;
}
if( stats.JoinedTime != joinedAt )
{
stats.JoinedTime = joinedAt;
stats.JoinedCount++;
}
stats.ShardId = this.CurrentShard.Id - 1;
stats.ServerName = pair.Value.Guild.Name;
stats.OwnerId = pair.Value.Guild.OwnerId;
if( pair.Value.Guild.Owner != null )
stats.OwnerName = pair.Value.Guild.Owner.GetUsername();
stats.IsDiscordPartner = pair.Value.Guild.VoiceRegionId.StartsWith("vip");
stats.UserCount = pair.Value.Guild.MemberCount;
if( string.IsNullOrEmpty(pair.Value.Config.InviteUrl) )
{
ServerContext dbContext = ServerContext.Create(this.DbConnectionString);
try
{
dbContext.ServerConfigurations.First(s => s.ServerId == pair.Value.Id).InviteUrl =
(await pair.Value.Guild.DefaultChannel.CreateInviteAsync(0)).Url;
}
catch(Exception) { }
dbContext.SaveChanges();
dbContext.Dispose();
}
}
}
private Task UpdateServerConfigs()
{
ServerContext dbContext = ServerContext.Create(this.DbConnectionString);
foreach( KeyValuePair<guid, Server> pair in this.Servers )
{
pair.Value.ReloadConfig(this.DbConnectionString, dbContext, this.Commands);
}
dbContext.Dispose();
return Task.CompletedTask;
}
private Task UpdateSubscriptions()
{
GlobalContext dbContext = GlobalContext.Create(this.DbConnectionString);
this.Subscribers?.Clear();
this.Subscribers = dbContext.Subscribers.ToDictionary(s => s.UserId);
this.PartneredServers?.Clear();
this.PartneredServers = dbContext.PartneredServers.ToDictionary(s => s.ServerId);
this.ValidSubscribers = this.Subscribers.Any() && this.PartneredServers.Any();
return Task.CompletedTask;
}
//Modules
private async Task InitModules()
{
List<Command> newCommands;
foreach( IModule module in this.Modules )
{
try
{
module.HandleException += async (e, d, id) =>
await LogException(e, "--Module." + module.ToString() + " | " + d, id);
newCommands = module.Init(this);
foreach( Command cmd in newCommands )
{
if( this.Commands.ContainsKey(cmd.Id) )
{
this.Commands[cmd.Id] = cmd;
continue;
}
this.Commands.Add(cmd.Id, cmd);
}
}
catch(Exception exception)
{
await LogException(exception, "--ModuleInit." + module.ToString());
}
}
}
private async Task UpdateModules()
{
IEnumerable<IModule> modules = this.Modules.Where(m => m.DoUpdate);
foreach( IModule module in modules )
{
if( this.GlobalConfig.LogDebug )
Console.WriteLine($"BotwinderClient: ModuleUpdate.{module.ToString()} triggered at: " + Utils.GetTimestamp(DateTime.UtcNow));
DateTime frameTime = DateTime.UtcNow;
if( this.DiscordClient.ConnectionState != ConnectionState.Connected ||
this.DiscordClient.LoginState != LoginState.LoggedIn )
break;
try
{
await module.Update(this);
}
catch(Exception exception)
{
await LogException(exception, "--ModuleUpdate." + module.ToString());
}
if( this.GlobalConfig.LogDebug )
Console.WriteLine($"BotwinderClient: ModuleUpdate.{module.ToString()} took: {(DateTime.UtcNow - frameTime).TotalMilliseconds} ms");
}
}
//Commands
private void GetCommandAndParams(string message, out string commandString, out string trimmedMessage, out string[] parameters)
{
trimmedMessage = "";
parameters = null;
MatchCollection regexMatches = this.RegexCommandParams.Matches(message);
if( regexMatches.Count == 0 )
{
commandString = message.Trim();
return;
}
commandString = regexMatches[0].Value;
if( regexMatches.Count > 1 )
{
trimmedMessage = message.Substring(regexMatches[1].Index).Trim('\"', ' ', '\n');
Match[] matches = new Match[regexMatches.Count];
regexMatches.CopyTo(matches, 0);
parameters = matches.Skip(1).Select(p => p.Value).ToArray();
for( int i = 0; i < parameters.Length; i++ )
parameters[i] = parameters[i].Trim('"');
}
}
private async Task<bool> HandleCommand(Server server, SocketTextChannel channel, SocketMessage message, string prefix)
{
GetCommandAndParams(message.Content.Substring(prefix.Length), out string commandString, out string trimmedMessage, out string[] parameters);
string originalCommandString = commandString;
if( this.GlobalConfig.LogDebug )
Console.WriteLine($"Command: {commandString} | {trimmedMessage}");
CommandOptions commandOptions = server.GetCommandOptions(commandString);
if( server.Commands.ContainsKey(commandString) ||
(server.CustomAliases.ContainsKey(commandString) &&
server.Commands.ContainsKey(commandString = server.CustomAliases[commandString].CommandId)) )
{
Command command = server.Commands[commandString];
if( command.IsAlias && !string.IsNullOrEmpty(command.ParentId) ) //Internal, not-custom alias.
command = server.Commands[command.ParentId];
CommandArguments args = new CommandArguments(this, command, server, channel, message, originalCommandString, trimmedMessage, parameters, commandOptions);
if( command.CanExecute(this, server, channel, message.Author as SocketGuildUser) )
return await command.Execute(args);
}
else if( server.CustomCommands.ContainsKey(commandString) ||
(server.CustomAliases.ContainsKey(commandString) &&
server.CustomCommands.ContainsKey(commandString = server.CustomAliases[commandString].CommandId)) )
{
if( server.CustomCommands[commandString].CanExecute(this, server, channel, message.Author as SocketGuildUser) )
return await HandleCustomCommand(server, server.CustomCommands[commandString], commandOptions, channel, message);
}
return false;
}
private async Task<bool> HandleCustomCommand(Server server, CustomCommand cmd, CommandOptions commandOptions, SocketTextChannel channel, SocketMessage message)
{
try
{
if( commandOptions != null && commandOptions.DeleteRequest &&
channel.Guild.CurrentUser.GuildPermissions.ManageMessages && !message.Deleted )
await message.DeleteAsync();
}catch( Exception ) { }
//todo - rewrite using string builder...
string msg = cmd.Response;
if( msg.Contains("{sender}") || msg.Contains("{{sender}}") )
{
msg = msg.Replace("{{sender}}", "<@{0}>").Replace("{sender}", "<@{0}>");
msg = string.Format(msg, message.Author.Id);
}
if( (msg.Contains("{mentioned}") || msg.Contains("{{mentioned}}")) && message.MentionedUsers != null )
{
string mentions = "";
SocketUser[] mentionedUsers = message.MentionedUsers.ToArray();
for( int i = 0; i < mentionedUsers.Length; i++ )
{
if( i != 0 )
mentions += (i == mentionedUsers.Length - 1) ? " and " : ", ";
mentions += "<@" + mentionedUsers[i].Id + ">";
}
if( string.IsNullOrEmpty(mentions) )
{
msg = msg.Replace("{{mentioned}}", "Nobody").Replace("{mentioned}", "Nobody");
}
else
{
msg = msg.Replace("{{mentioned}}", "{0}").Replace("{mentioned}", "{0}");
msg = string.Format(msg, mentions);
}
}
if( server.Config.IgnoreEveryone )
msg = msg.Replace("@everyone", "@-everyone").Replace("@here", "@-here");
await SendRawMessageToChannel(channel, msg);
return true;
}
// Guild events
private async Task OnGuildJoined(SocketGuild guild)
{
try
{
await OnGuildAvailable(guild);
string msg = Localisation.SystemStrings.GuildJoined;
if( !IsPartner(guild.Id) && !IsSubscriber(guild.OwnerId) )
msg += Localisation.SystemStrings.GuildJoinedTrial;
try
{
await guild.Owner.SendMessageSafe(msg);
}
catch(Exception) { }
}
catch(Exception exception)
{
await LogException(exception, "--OnGuildJoined", guild.Id);
}
}
#pragma warning disable 1998
private async Task OnGuildUpdated(SocketGuild originalGuild, SocketGuild updatedGuild)
#pragma warning restore 1998
{
if( !this.Servers.ContainsKey(originalGuild.Id) )
return;
this.Servers[originalGuild.Id].Guild = updatedGuild;
}
private async Task OnGuildLeft(SocketGuild guild)
{
try
{
if( !this.Servers.ContainsKey(guild.Id) )
return;
for(int i = this.CurrentOperations.Count -1; i >= 0; i--)
{
if( this.CurrentOperations[i].CommandArgs.Server.Id == guild.Id )
this.CurrentOperations[i].Cancel();
}
this.Servers.Remove(guild.Id);
}
catch(Exception exception)
{
await LogException(exception, "--OnGuildLeft", guild.Id);
}
}
private async Task OnGuildAvailable(SocketGuild guild)
{
ServerContext dbContext = null;
try
{
while( !this.IsInitialized )
await Task.Delay(1000);
Server server;
dbContext = ServerContext.Create(this.DbConnectionString);
if( this.Servers.ContainsKey(guild.Id) )
{
server = this.Servers[guild.Id];
server.ReloadConfig(this.DbConnectionString, dbContext, this.Commands);
}
else
{
server = new Server(guild);
server.LoadConfig(this.DbConnectionString, dbContext, this.Commands);
server.Localisation = GlobalContext.Create(this.DbConnectionString).Localisations.FirstOrDefault(l => l.Id == server.Config.LocalisationId);
this.Servers.Add(server.Id, server);
}
}
catch(Exception exception)
{
await LogException(exception, "--OnGuildAvailable", guild.Id);
}
finally
{
dbContext?.Dispose();
}
}
private async Task BailBadServers()
{
try
{
List<Server> serversToLeave = new List<Server>();
foreach( KeyValuePair<guid, Server> pair in this.Servers )
{
try
{
//Trial count exceeded
ServerStats stats;
lock(this.DbLock)
stats = this.ServerDb.ServerStats.FirstOrDefault(s => s.ServerId == pair.Value.Id);
bool joinedCountExceeded = stats != null && stats.JoinedCount > this.GlobalConfig.VipTrialJoins;
bool trialTimeExceeded = pair.Value.Guild.CurrentUser?.JoinedAt != null &&
DateTime.UtcNow - pair.Value.Guild.CurrentUser.JoinedAt.Value.ToUniversalTime() > TimeSpan.FromHours(this.GlobalConfig.VipTrialHours);
//Partnered servers
if( !(IsPartner(pair.Value.Id) || IsSubscriber(pair.Value.Guild.OwnerId)) &&
(joinedCountExceeded || trialTimeExceeded) )
{
if( serversToLeave.Contains(pair.Value) )
continue;
serversToLeave.Add(pair.Value);
if( !this.LeaveNotifiedOwners.Contains(pair.Value.Guild.OwnerId) )
{
this.LeaveNotifiedOwners.Add(pair.Value.Guild.OwnerId);
try
{
await pair.Value.Guild.Owner.SendMessageSafe(Localisation.SystemStrings.VipPmLeaving);
}
catch(Exception) { }
}
continue;
}
//Blacklisted servers
lock(this.DbLock)
if( !serversToLeave.Contains(pair.Value) &&
this.GlobalDb.Blacklist.Any(b => b.Id == pair.Value.Id || b.Id == pair.Value.Guild.OwnerId) )
{
serversToLeave.Add(pair.Value);
continue;
}
}
catch(Exception exception)
{
await LogException(exception, "--BailBadServers", pair.Value.Id);
}
}
foreach( Server server in serversToLeave )
{
await server.Guild.LeaveAsync();
}
}
catch(Exception exception)
{
await LogException(exception, "--BailBadServers");
}
}
private Task OnGuildMembersDownloaded(SocketGuild guild)
{
ServerContext dbContext = ServerContext.Create(this.DbConnectionString);
List<Username> usernames = dbContext.Usernames.Where(u => u.ServerId == guild.Id).ToList();
List<Nickname> nicknames = dbContext.Nicknames.Where(u => u.ServerId == guild.Id).ToList();
foreach( SocketGuildUser user in guild.Users )
{
if( !usernames.Any(u => u.UserId == user.Id && u.Name == user.Username) )
{
Username username = new Username(){
ServerId = user.Guild.Id,
UserId = user.Id,
Name = user.Username
};
usernames.Add(username);
dbContext.Usernames.Add(username);
}
if( !string.IsNullOrEmpty(user.Nickname) &&
!nicknames.Any(u => u.UserId == user.Id && u.Name == user.Nickname) )
{
Nickname nickname = new Nickname(){
ServerId = user.Guild.Id,
UserId = user.Id,
Name = user.Nickname
};
nicknames.Add(nickname);
dbContext.Nicknames.Add(nickname);
}
}
dbContext.SaveChanges();
dbContext.Dispose();
return Task.CompletedTask;
}
// User events