Esempio n. 1
0
            public static async Task <bool> CheckForRpcs3Updates(DiscordClient discordClient, DiscordChannel channel, string sinceCommit = null)
            {
                var info = await client.GetUpdateAsync(Config.Cts.Token, sinceCommit).ConfigureAwait(false);

                if (info?.ReturnCode != 1 && sinceCommit != null)
                {
                    info = await client.GetUpdateAsync(Config.Cts.Token).ConfigureAwait(false);
                }
                var embed = await info.AsEmbedAsync().ConfigureAwait(false);

                if (info == null || embed.Color == Config.Colors.Maintenance)
                {
                    embed = await CachedUpdateInfo.AsEmbedAsync().ConfigureAwait(false);
                }
                else
                {
                    CachedUpdateInfo = info;
                }
                if (channel != null)
                {
                    await channel.SendMessageAsync(embed : embed.Build()).ConfigureAwait(false);
                }
                var updateLinks = info?.LatestBuild?.Pr;

                if (!string.IsNullOrEmpty(updateLinks) && lastUpdateInfo != updateLinks && updateCheck.Wait(0))
                {
                    try
                    {
                        var compatChannel = await discordClient.GetChannelAsync(Config.BotChannelId).ConfigureAwait(false);

                        await compatChannel.SendMessageAsync(embed : embed.Build()).ConfigureAwait(false);

                        lastUpdateInfo = updateLinks;
                        using (var db = new BotDb())
                        {
                            var currentState = await db.BotState.FirstOrDefaultAsync(k => k.Key == Rpcs3UpdateStateKey).ConfigureAwait(false);

                            if (currentState == null)
                            {
                                db.BotState.Add(new BotState {
                                    Key = Rpcs3UpdateStateKey, Value = updateLinks
                                });
                            }
                            else
                            {
                                currentState.Value = updateLinks;
                            }
                            await db.SaveChangesAsync(Config.Cts.Token).ConfigureAwait(false);

                            return(true);
                        }
                    }
                    catch (Exception e)
                    {
                        Config.Log.Warn(e, "Failed to check for RPCS3 update info");
                    }
                }
Esempio n. 2
0
            public static async Task <bool> CheckForRpcs3Updates(DiscordClient discordClient, DiscordChannel channel, string sinceCommit = null, DiscordMessage emptyBotMsg = null)
            {
                var updateAnnouncement        = channel is null;
                var updateAnnouncementRestore = emptyBotMsg != null;
                var info = await client.GetUpdateAsync(Config.Cts.Token, sinceCommit).ConfigureAwait(false);

                if (info?.ReturnCode != 1 && sinceCommit != null)
                {
                    info = await client.GetUpdateAsync(Config.Cts.Token).ConfigureAwait(false);
                }

                if (updateAnnouncementRestore && info?.CurrentBuild != null)
                {
                    info.LatestBuild = info.CurrentBuild;
                }
                var embed = await info.AsEmbedAsync(discordClient, updateAnnouncement).ConfigureAwait(false);

                if (info == null || embed.Color.Value.Value == Config.Colors.Maintenance.Value)
                {
                    if (updateAnnouncementRestore)
                    {
                        Config.Log.Debug($"Failed to get update info for commit {sinceCommit}: {JsonConvert.SerializeObject(info)}");
                        return(false);
                    }

                    embed = await CachedUpdateInfo.AsEmbedAsync(discordClient, updateAnnouncement).ConfigureAwait(false);
                }
                else if (!updateAnnouncementRestore)
                {
                    if (CachedUpdateInfo.LatestBuild?.Datetime is string previousBuildTimeStr &&
                        info.LatestBuild?.Datetime is string newBuildTimeStr &&
                        DateTime.TryParse(previousBuildTimeStr, out var previousBuildTime) &&
                        DateTime.TryParse(newBuildTimeStr, out var newBuildTime) &&
                        newBuildTime > previousBuildTime)
                    {
                        CachedUpdateInfo = info;
                    }
                    else
                    {
                        return(true);
                    }
                }
Esempio n. 3
0
            public static async Task <bool> CheckForRpcs3Updates(DiscordClient discordClient, DiscordChannel channel, string sinceCommit = null, DiscordMessage emptyBotMsg = null)
            {
                var info = await client.GetUpdateAsync(Config.Cts.Token, sinceCommit).ConfigureAwait(false);

                if (info?.ReturnCode != 1 && sinceCommit != null)
                {
                    info = await client.GetUpdateAsync(Config.Cts.Token).ConfigureAwait(false);
                }

                if (emptyBotMsg != null && info?.CurrentBuild != null)
                {
                    info.LatestBuild = info.CurrentBuild;
                }
                var embed = await info.AsEmbedAsync().ConfigureAwait(false);

                if (info == null || embed.Color == Config.Colors.Maintenance)
                {
                    if (emptyBotMsg != null)
                    {
                        return(false);
                    }

                    embed = await CachedUpdateInfo.AsEmbedAsync().ConfigureAwait(false);
                }
                else if (emptyBotMsg == null)
                {
                    CachedUpdateInfo = info;
                }
                if (channel != null)
                {
                    await channel.SendMessageAsync(embed : embed.Build()).ConfigureAwait(false);
                }
                else if (emptyBotMsg != null)
                {
                    if (embed.Color == Config.Colors.Maintenance)
                    {
                        return(false);
                    }

                    Config.Log.Debug($"Restoring update announcement for build {sinceCommit}: {embed.Title}\n{embed.Description}");
                    await emptyBotMsg.ModifyAsync(embed : embed.Build()).ConfigureAwait(false);

                    return(true);
                }

                var updateLinks = info?.LatestBuild?.Pr?.ToString();

                if (!string.IsNullOrEmpty(updateLinks) &&
                    lastUpdateInfo != updateLinks &&
                    await updateCheck.WaitAsync(0).ConfigureAwait(false))
                {
                    try
                    {
                        var compatChannel = await discordClient.GetChannelAsync(Config.BotChannelId).ConfigureAwait(false);

                        var botMember = discordClient.GetMember(compatChannel.Guild, discordClient.CurrentUser);
                        if (botMember == null)
                        {
                            return(false);
                        }

                        if (!compatChannel.PermissionsFor(botMember).HasPermission(Permissions.SendMessages))
                        {
                            NewBuildsMonitor.Reset();
                            return(false);
                        }

                        embed.Title = $"[New Update] {embed.Title}";
                        await compatChannel.SendMessageAsync(embed : embed.Build()).ConfigureAwait(false);

                        lastUpdateInfo = updateLinks;
                        using (var db = new BotDb())
                        {
                            var currentState = await db.BotState.FirstOrDefaultAsync(k => k.Key == Rpcs3UpdateStateKey).ConfigureAwait(false);

                            if (currentState == null)
                            {
                                db.BotState.Add(new BotState {
                                    Key = Rpcs3UpdateStateKey, Value = updateLinks
                                });
                            }
                            else
                            {
                                currentState.Value = updateLinks;
                            }
                            await db.SaveChangesAsync(Config.Cts.Token).ConfigureAwait(false);

                            NewBuildsMonitor.Reset();
                            return(true);
                        }
                    }
                    catch (Exception e)
                    {
                        Config.Log.Warn(e, "Failed to check for RPCS3 update info");
                    }
                }