Example #1
0
 private void Twitch_OnStreamOnline(object sender, StreamEventArgs e)
 {
     try
     {
         Task.Run(async() =>
         {
             await StreamOnline(e);
         });
     }
     catch (Exception ex)
     {
         _logger.Error(ex, $"Error while pushing event");
     }
 }
Example #2
0
        private async Task StreamOnline(StreamEventArgs e)
        {
            try
            {
                using var db = Database.Open();
                var dbStream = db.Streams.FirstOrDefault(p => p.StreamId == Convert.ToUInt64(e.Stream.Id) && p.AnnouncedGuildId == e.GuildId);
                if (dbStream != null)
                {
                    return;
                }

                await db.Streams.AddAsync(new StreamEntity { StreamId = Convert.ToUInt64(e.Stream.Id), StartTime = e.Stream.CreatedAt, Title = e.Stream.Channel.Status, TwitchUserId = Convert.ToUInt64(e.Stream.Channel.Id), AnnouncedGuildId = e.GuildId });

                await db.SaveChangesAsync();

                var dbGuild = db.Guilds.AsQueryable().FirstOrDefault(p => p.GuildId == e.GuildId);
                if (dbGuild == null)
                {
                    return;
                }

                if (dbGuild.StreamChannelId != null)
                {
                    var guild = _client.Guilds.FirstOrDefault(p => p.Id == dbGuild.GuildId);
                    if (guild == null)
                    {
                        return;
                    }
                    if (!(guild.Channels.FirstOrDefault(p => p.Id == dbGuild.StreamChannelId) is SocketTextChannel channel))
                    {
                        return;
                    }

                    var embed  = new EmbedBuilder();
                    var author = new EmbedAuthorBuilder
                    {
                        Name    = e.Stream.Channel.DisplayName,
                        IconUrl = e.Stream.Channel.Logo
                    };
                    embed.WithAuthor(author);
                    embed.WithTitle(e.Stream.Channel.Status);
                    embed.WithUrl($"https://www.twitch.tv/{e.Stream.Channel.Name}");
                    if (!string.IsNullOrWhiteSpace(e.Stream.Channel.Logo))
                    {
                        embed.WithThumbnailUrl(e.Stream.Channel.Logo);
                    }
                    if (!string.IsNullOrWhiteSpace(e.Stream.Game))
                    {
                        embed.AddField("Game", e.Stream.Game, true);
                    }
                    embed.AddField("Viewers", e.Stream.Viewers.ToString(), true);
                    var ThumbnailUrl = e.Stream.Preview.Large.Replace("{width}", "1280").Replace("{height}", "720");
                    if (!string.IsNullOrWhiteSpace(ThumbnailUrl))
                    {
                        embed.WithImageUrl(ThumbnailUrl);
                    }
                    if (e.Stream.Channel.Name == "swaight")
                    {
                        await channel.SendMessageAsync($"Hi {guild.EveryoneRole.Mention}! Ich bin live auf https://www.twitch.tv/{e.Stream.Channel.Name} Schaut mal vorbei :)", false, embed.Build());
                    }
                    else
                    {
                        await channel.SendMessageAsync($"Hi {guild.EveryoneRole.Mention}! {e.Stream.Channel.DisplayName} ist live auf https://www.twitch.tv/{e.Stream.Channel.Name} Schaut mal vorbei :)", false, embed.Build());
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex, $"Error while sending stream announcement");
            }
        }