Exemple #1
0
        internal static async Task ScanChannels(DiscordClient client)
        {
            while (bRunning)
            {
                var lastId    = ulong.MinValue;
                var lastGuild = ulong.MinValue;

                Ttv.Logger.LogInformation("Start Cycle");
                try
                {
                    var streams = await da.GetStreamers();

                    foreach (DataRow stream in streams.Rows)
                    {
                        try
                        {
                            var discordId = ulong.Parse(stream["discordId"].ToString());
                            var guild     = client.GetGuildAsync(ulong.Parse(stream["guildId"].ToString()),
                                                                 false).Result;
                            Ttv.Logger.LogDebug($"Checking {stream["name"]} in {guild.Name}");
                            if (!IsMemberStillHere(discordId, client, guild))
                            {
                                continue;
                            }
                            var streamId = twitch.GetChannelId(stream["name"].ToString());
                            if (string.IsNullOrEmpty(streamId) || !twitch.IsOnline(streamId))
                            {
                                continue;
                            }
                            var message = string.Empty;
                            switch (stream["approved"].ToString())
                            {
                            case "1":
                            {
                                message = $"🔴 Hey @here! <@!{discordId}> Is Live!";
                                break;
                            }

                            default:
                            {
                                message = $"🔴 Hey All! <@!{discordId}> Is Live!";
                                break;
                            }
                            }

                            try
                            {
                                var embed = twitch.BuildPromoEmbed(stream["name"].ToString());
                                if (embed == null)
                                {
                                    throw new Exception("No Embed Created");
                                }
                                var channel = GetChannelFromId(client, ulong.Parse(stream["channelId"].ToString()));

                                if (channel.GuildId == lastGuild && discordId == lastId)
                                {
                                    continue;
                                }

                                await channel.SendMessageAsync($"{message}", embed : embed);
                                await LogAction($"{stream["name"]} has been promoted for <@!{discordId}> [{discordId}]",
                                                client);

                                lastId    = discordId;
                                lastGuild = channel.GuildId.Value;
                            }
                            catch (Exception)
                            {
                                Ttv.Logger.LogInformation($"No Embed Created For {stream["name"]}, Will skip.");
                                continue;
                            }

                            var now = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm");
                            da.UpdateStream(stream["id"].ToString(), now);
                        }
                        catch (Exception e)
                        {
                            if (e.Message.Contains("One or more errors occurred. (Not found: 404)"))
                            {
                                continue;
                            }
                            Ttv.Logger.LogCritical($"{e}");
                            await LogAction($"TwitchTV Module encountered an error while scanning channel {stream["name"]} \n {e.Message}\n```{e}```", client);
                        }
                    }
                }
                catch (Exception e)
                {
                    Ttv.Logger.LogCritical($"{e}");
                    await LogAction($"TwitchTV Module encountered an error in task `ScanChannels() ` \n {e.Message}\n```{e}```", client);
                }

                Thread.Sleep(TimeSpan.FromSeconds(30));
            }
        }