/// <summary> /// Handles announcement of a user to specified channels. /// </summary> /// <param name="user">user to be announced.</param> /// <param name="channels">List of channels with module enabled.</param> /// <returns></returns> private async Task AnnounceUserHandler(SocketGuildUser user, List <StreamerChannelSettings> channels) { try { var announcements = new List <Task <StreamAnnouncerMessage> >(); // Announce to all enabled channels in guild and store message in list. foreach (var c in channels) { var message = new StreamAnnouncerMessage { UserId = user.Id, ChannelId = c.ChannelId }; StreamingList[user.Guild.Id].Add(message); announcements.Add(AnnounceUser(user, message, c.ChannelId)); } var messages = await Task.WhenAll(announcements); using (var scope = _scopeFactory.CreateScope()) { var messageService = scope.ServiceProvider.GetRequiredService <IEntityService <StreamAnnouncerMessage> >(); await messageService.CreateBulk(messages); } } catch (Exception e) { Logger.LogError( $"Streamer Module[AnnounceUserHandler]: Error occurred '{e.Message}'. Guild ID: {user.Guild.Id}, Channels: {channels}, User ID: {user.Id}."); } }
/// <summary> /// Remove announcement message from channel if necessary. /// </summary> /// <param name="user">User whom stopped streaming</param> /// <param name="m">Message to delete </param> private async Task DeleteMessageAsync(SocketGuildUser user, StreamAnnouncerMessage m) { Logger.LogDebug( $"Streamer Module: Deleting streaming message from {user.Username}. Guild ID: {m.GuildId}, " + $"Channel ID: {m.ChannelId}, User ID: {m.UserId}, Message ID: {m.MessageId}."); // Delete message. var message = await user.Guild.GetTextChannel(m.ChannelId).GetMessageAsync(m.MessageId); await message.DeleteAsync(); }
/// <summary> /// Announces the users stream to channel. /// </summary> /// <param name="user">User to be announced</param> /// <param name="m">Message from streaming list, this is so it's MessageId can be set.</param> /// <param name="channelId">Id of the channel to announce to.</param> /// <returns></returns> private async Task <StreamAnnouncerMessage> AnnounceUser(SocketGuildUser user, StreamAnnouncerMessage m, ulong channelId) { var streamingGame = (StreamingGame)user.Activity; // Build the embedded message. var embed = new EmbedBuilder() .WithTitle($"{user.Username} is now live!") .WithDescription($"{streamingGame.Url} - {user.Mention}") .WithColor(new Color(0x4A90E2)) .WithThumbnailUrl(user.GetAvatarUrl()) .AddField("Title", user.Activity.Name, true).Build(); // Announce the user to the channel specified in settings. var messageData = await user.Guild.GetTextChannel(channelId) .SendMessageAsync("", embed: embed); var messageId = messageData.Id; // Sets MessageId in hashset, as hashset holds reference to the message param. m.MessageId = messageId; // Sets navigation property/foreign key. m.GuildId = user.Guild.Id; Logger.LogDebug($"Streamer Module: Announcing user {user.Username}. Guild ID: {m.GuildId}, " + $"Channel ID: {m.ChannelId}, User ID: {m.UserId}, Message ID: {m.MessageId}."); return(m); }