Exemple #1
0
        public static async Task StreamPost(ISocketMessageChannel chan, IUser user, TwitchLib.Api.V5.Models.Streams.Stream stream, int loud = 0)
        {
            var url   = stream.Channel.Url;
            var embed = new EmbedBuilder();

            embed.WithAuthor(new EmbedAuthorBuilder()
            {
                Name = $"{user.Username} is streaming on Twitch!", IconUrl = user.GetAvatarUrl(), Url = url
            });
            embed.WithTitle(stream.Channel.Status);
            embed.WithUrl(url);
            embed.WithColor(new Color(100, 65, 165));
            embed.AddField($"Playing {stream.Channel.Game} for {stream.Viewers} viewers!", $"[Watch Stream]({url})");
            embed.WithImageUrl(stream.Preview.Large + $"?time={(Int32)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds}");
            embed.WithFooter("Skynet is always watching!");
            var    embeded = embed.Build();
            string mention = "";

            switch (loud)
            {
            case 0:
                mention = "";
                break;

            case 1:
                mention = "@here";
                break;

            case 2:
                mention = "@everyone";
                break;
            }
            await chan.SendMessageAsync(mention, false, embeded);
        }
Exemple #2
0
        private static void Monitor_OnStreamOnline(object sender, OnStreamOnlineArgs args)
        {
            Task.Run(async() =>
            {
                // TODO: Use the 'Date' field in the link database to determine whether we should post it.

                var links = Links.Values.Where(e => string.Equals(e.Value.Split("|", StringSplitOptions.RemoveEmptyEntries).FirstOrDefault(), args.Channel, StringComparison.CurrentCultureIgnoreCase));
                foreach (var link in links)
                {
                    try
                    {
                        var linkValues         = link.Value.Split("|", StringSplitOptions.RemoveEmptyEntries);
                        var streamName         = linkValues.FirstOrDefault();
                        long linkStreamId      = -1;
                        var linkDateCreated    = DateTime.MinValue;
                        var linkDateUpdated    = DateTime.MinValue;
                        ulong discordMessageId = 0;
                        if (linkValues.Length > 1)
                        {
                            long.TryParse(linkValues[1], out linkStreamId);
                        }
                        if (linkValues.Length > 2)
                        {
                            if (long.TryParse(linkValues[2], out long ticks))
                            {
                                linkDateCreated = new DateTime(ticks, DateTimeKind.Utc);
                            }
                        }
                        if (linkValues.Length > 3)
                        {
                            if (long.TryParse(linkValues[3], out long ticks))
                            {
                                linkDateUpdated = new DateTime(ticks, DateTimeKind.Utc);
                            }
                        }
                        if (linkValues.Length > 4)
                        {
                            ulong.TryParse(linkValues[4], out discordMessageId);
                        }

                        // Attempt to retrieve the V5 stream data for more detailed info.
                        TwitchLib.Api.V5.Models.Streams.Stream stream = null;
                        try
                        {
                            stream = (await Ditto.Twitch.V5.Streams.GetStreamByUserAsync(args.Stream.UserId, args.Stream.Type).ConfigureAwait(false))?.Stream;
                        }
                        catch { }

                        if (stream != null)
                        {
                            if (discordMessageId == 0 || (stream.Id != linkStreamId && stream.CreatedAt > linkDateCreated))
                            {
                                linkStreamId    = stream.Id;
                                linkDateCreated = stream.CreatedAt;
                                linkDateUpdated = DateTime.UtcNow;

                                Log.Debug($"Twitch | {stream?.Channel?.Name} went live (Id: {stream?.Id} at {stream?.CreatedAt.ToLongTimeString()}).");

                                var embedBuilder = GetTwitchEmbedMessage(args.Stream, stream, link);

                                if (link.Channel != null)
                                {
                                    var message = await link.Channel.EmbedAsync(embedBuilder,
                                                                                options: new RequestOptions()
                                    {
                                        RetryMode = RetryMode.RetryRatelimit | RetryMode.RetryTimeouts
                                    }
                                                                                ).ConfigureAwait(false);
                                    discordMessageId = message?.Id ?? 0;
                                }
                                else
                                {
                                    Log.Debug($"Twitch | Link #{link.Id} doesn't have a channel, clean-up database?");
                                }
                            }
                            else
                            {
                                var discordMessage = (await link.Channel.GetMessageAsync(discordMessageId,
                                                                                         CacheMode.AllowDownload,
                                                                                         new RequestOptions()
                                {
                                    RetryMode = RetryMode.AlwaysRetry
                                }
                                                                                         ).ConfigureAwait(false)) as IUserMessage;

                                if (discordMessage?.Author?.IsBot == true)
                                {
                                    var embedBuilder = GetTwitchEmbedMessage(args.Stream, stream, link);
                                    await discordMessage.ModifyAsync(x => x.Embed = embedBuilder.Build(), new RequestOptions()
                                    {
                                        RetryMode = RetryMode.AlwaysRetry
                                    }).ConfigureAwait(false);
                                    discordMessageId = discordMessage.Id;
                                    linkDateCreated  = stream.CreatedAt;
                                    linkDateUpdated  = DateTime.UtcNow;
                                }
                                else
                                {
                                    discordMessageId = 0;
                                    linkDateCreated  = DateTime.MinValue;
                                    linkDateUpdated  = DateTime.MinValue;
                                }
                            }

                            // Update link
                            link.Value = $"{streamName}|{stream.Id}|{linkDateCreated.ToUniversalTime().Ticks}|{linkDateUpdated.Ticks}|{discordMessageId}";
                            await Ditto.Database.DoAsync((uow) =>
                            {
                                uow.Links.Update(link);
                            }).ConfigureAwait(false);
                        }
                    }
                    catch { }
                }
            });
        }
Exemple #3
0
        private static EmbedBuilder GetTwitchEmbedMessage(TwitchLib.Api.Helix.Models.Streams.Stream stream, TwitchLib.Api.V5.Models.Streams.Stream streamV5, Link link)
        {
            var channelName    = (streamV5?.Channel?.DisplayName ?? streamV5?.Channel?.Name ?? stream?.UserName ?? "Unknown");
            var streamImageUrl = stream.ThumbnailUrl.Replace("{width}", "800").Replace("{height}", "600");
            var game           = streamV5?.Game;

            if (string.IsNullOrEmpty(game))
            {
                game = $"Unknown ({stream?.GameId}";
            }

            var embedBuilder = new EmbedBuilder()
                               //.WithTitle($"[Twitch] {args.Channel}")
                               //.WithDescription(args.Stream.Title)
                               .WithTitle(stream.Title)
                               .WithFields(
                //new EmbedFieldBuilder().WithName("Description").WithValue(args.Stream.Title).WithIsInline(false),
                new EmbedFieldBuilder().WithName("Playing").WithValue(game).WithIsInline(true),
                new EmbedFieldBuilder().WithName("Viewers").WithValue(stream.ViewerCount).WithIsInline(true)
                )
                               .WithFooter(efb => efb.WithText($"started at {stream.StartedAt:dd-MM-yyyy HH:mm:ss}"))
                               .WithUrl($"https://twitch.tv/{channelName}")
                               .WithThumbnailUrl(streamImageUrl)
                               .WithAuthor(new EmbedAuthorBuilder()
                                           .WithIconUrl(streamV5?.Channel?.Logo ?? "")
                                           .WithName($"[Twitch] {channelName}")
                                           .WithUrl($"https://twitch.tv/{channelName}")
                                           )
                               .WithTwitchColour(link.Guild)
            ;


            return(embedBuilder);
        }
Exemple #4
0
        private async Task SetupEmbedMessageAsync(EmbedBuilder eb, TwitchLib.Api.V5.Models.Streams.StreamByUser e, TwitchLib.Api.V5.Models.Streams.Stream s)
        {
            try
            {
                if (s == null && e == null || eb == null)
                {
                    return;
                }
                TwitchLib.Api.V5.Models.Streams.StreamByUser ee = null;
                if (e != null)
                {
                    ee = await API.V5.Streams.GetStreamByUserAsync(e.Stream.Channel.Id);
                }

                string twitchURL   = ee?.Stream.Channel.Url ?? s?.Channel.Url;
                string channelID   = ee?.Stream.Channel.Id ?? s?.Channel.Id;
                string channelName = ee?.Stream.Channel.DisplayName ?? s?.Channel.DisplayName;
                string status      = ee?.Stream.Channel.Status ?? s?.Channel.Status;
                string game        = ee?.Stream.Channel.Game ?? s?.Channel.Game;
                int    vCount      = ee?.Stream.Viewers ?? s.Viewers;

                if (Setup.DiscordAnnounceChannel == 0)
                {
                    return;
                }
                if (_client.ConnectionState == ConnectionState.Connected)
                {
                    string here = "";

                    if (_botOnlineTime.AddSeconds(30) <= DateTime.Now)
                    {
                        here = "@here ";
                    }
                    here += $"\nTwitch (*{twitchURL}*)";
                    here  = here.Insert(0, $"**{channelName} is live!** ");

                    await SendEmbedAsync(Setup.DiscordAnnounceChannel, eb, here, channelID, status, game, vCount);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }