public static async Task <bool> SendRetryLinkMessageAsync(LinkType linkType, int repostCount, Exception ex = null) { if (Ditto.Running && await Ditto.IsClientConnectedAsync().ConfigureAwait(false)) { Log.Debug($"Attempting to repost link ({linkType.ToString()})... {repostCount}{(ex == null ? string.Empty : $" | {ex}")}"); await Task.Delay(1000).ConfigureAwait(false); return(true); }
public static void Start() { if (!Initialized) { Initialized = true; if (_cancellationTokenSource.IsCancellationRequested) { _cancellationTokenSource = new CancellationTokenSource(); Running = false; } if (!Running) { Running = true; try { var _ = Task.Run(async() => { while (Running) { try { if ((DateTime.Now - _lastTime) > Delay) { // Sort items based on priority and date added //if (Ditto.Running && Ditto.Client != null // && Ditto.Client.ConnectionState == ConnectionState.Connected // && Ditto.Client.LoginState == LoginState.LoggedIn) //{ // var items = _playingStatusItems.OrderByDescending(e => e.Priority).ThenByDescending(e => e.DateAdded); // foreach (var item in items) // { // var result = await item.Execute(Ditto.Client).ConfigureAwait(false); // if (result != null) // { // if (Ditto.Running && Ditto.Client != null // && Ditto.Client.ConnectionState == ConnectionState.Connected // && Ditto.Client.LoginState == LoginState.LoggedIn) // { // await Ditto.Client.SetGameAsync(result.Length == 0 ? null : result); // } // break; // } // } //} if (await Ditto.IsClientConnectedAsync()) { var items = _playingStatusItems.OrderByDescending(e => e.Priority).ThenByDescending(e => e.DateAdded); foreach (var item in items) { var result = await item.Execute(await Ditto.Client.DoAsync(c => c)).ConfigureAwait(false); if (result != null) { if (await Ditto.IsClientConnectedAsync()) { await Ditto.Client.DoAsync(c => c.SetGameAsync(result.Length == 0 ? null : result) ).ConfigureAwait(false); } break; } } } _lastTime = DateTime.Now; } } catch (Exception ex) { Log.Error(ex); } await Task.Delay(100).ConfigureAwait(false); } }, _cancellationTokenSource.Token); } catch (Exception ex) when(ex is OperationCanceledException || ex is TaskCanceledException) { if (_cancellationTokenSource.IsCancellationRequested) { _cancellationTokenSource = new CancellationTokenSource(); } } } } }
static LinkUtility() { //On client connected Ditto.Connected += () => { _links?.Clear(); _cancellationTokenSource?.Cancel(); _cancellationTokenSource = new CancellationTokenSource(); Ditto.Database.ReadAsync((uow) => { _links = new ConcurrentDictionary <int, Link>( uow.Links.GetAllWithLinks() .Select(i => new KeyValuePair <int, Link>(i.Id, i)) ); }); var _ = Task.Run(async() => { while (Ditto.Running) { var databaseModified = false; var tasks = new List <Task>(); var links = _links.Select(i => i.Value).ToList(); foreach (var link in links) { if (!(Ditto.Running && await Ditto.IsClientConnectedAsync().ConfigureAwait(false))) { break; } tasks.Add(Task.Run(async() => { // Verify that we have access to the channel var channel = link.Channel; if (channel != null && channel.GuildId == link.GuildId && (await Ditto.Client.DoAsync((c) => c.GetPermissionsAsync(channel))).HasAccess() ) { var linkItems = await ReadAndPostLinkAsync(link).ConfigureAwait(false); if (linkItems.Count() > 0) { link.Links.AddRange(linkItems); databaseModified = true; } } else { Log.Debug($"Could not access the channel {channel?.Guild?.Name}:{channel?.Name}"); } })); } await Task.WhenAll(tasks).ConfigureAwait(false); if (databaseModified) { await Ditto.Database.WriteAsync(uow => { uow.Links.UpdateRange(links); }).ConfigureAwait(false); } await Task.Delay(500).ConfigureAwait(false); } }); return(Task.CompletedTask); }; // On client disconnected Ditto.Exit += () => { _links?.Clear(); _cancellationTokenSource?.Cancel(); return(Task.CompletedTask); }; }