private async Task Client_MessageReactionAddedAsync(MessageReactionAddEventArgs e) { try { if (e.Channel.Id != ArtChannel.Id) { return; } // check to see if this is the emote we echo to. if (!Config.ReactionEmojiIDs.Contains(e.Emoji.Id)) { return; } // get the memver and the perms var member = await e.Channel.Guild.GetMemberAsync(e.User.Id); var msg = await e.Channel.GetMessageAsync(e.Message.Id); var perms = member.PermissionsIn(EchoChannel); // check if the member has the needed permissions if (!perms.HasPermission(Config.NeededPerm)) { return; } // check if this message has already been cached if (EchoedCache.Any(ec => ec == e.Message.Id)) { return; } var imageUrl = ""; if (msg.Attachments.Count > 0) { if (msg.Attachments[0].Width != 0) { imageUrl = msg.Attachments[0].Url; } } // shouldn't need this but just incase. if (imageUrl == null) { imageUrl = ""; } DiscordMember artPoster = null; try { artPoster = await ArtChannel.Guild.GetMemberAsync(msg.Author.Id); } catch (Exception exc) { new LogWriter($"{exc.Message}\n{exc.StackTrace}\n\nAuthor ID:{msg.Author.Id}"); Client.DebugLogger.LogMessage(LogLevel.Error, "Bot", exc.Message, DateTime.Now); Client.DebugLogger.LogMessage(LogLevel.Error, "Bot", "That user is nolonger present in this server", DateTime.Now); return; } try { // if we dont have attachments check the the message for links if (msg.Attachments.Count == 0) { MatchCollection ms = Regex.Matches(msg.Content, @"(www.+|http.+)([\s]|$)"); if (ms.Count > 0 && IsImageUrl(ms[0].Value.ToString())) { imageUrl = ms[0].Value.ToString(); } } } catch (Exception ex) { imageUrl = ""; Client.DebugLogger.LogMessage(LogLevel.Error, "Bot", ex.Message, DateTime.Now); new LogWriter($"{ex.Message}\n{ex.StackTrace}\n\nAuthor ID:{msg.Author.Id}"); } try { switch (imageUrl) { // if theres no attachments just send the message content // TODO: in the future check to see if there are any links and try grabing the images from them // and display said image in the embed. case "": var eb = new DiscordEmbedBuilder { Title = $"Some amazing art by {artPoster.DisplayName}! ({artPoster.Username}#{artPoster.Discriminator})", Description = msg.Content, Footer = new EmbedFooter { Text = msg.Timestamp.ToString(), IconUrl = artPoster.AvatarUrl }, Color = new DiscordColor(Config.EmbedColor) }; await Client.SendMessageAsync(EchoChannel, "", false, eb); AddToCache(msg.Id); break; // sen a normal embed with a image. default: var ebImage = new DiscordEmbedBuilder { Title = $"Some amazing art by {artPoster.DisplayName}! ({artPoster.Username}#{artPoster.Discriminator})", Description = msg.Content, ImageUrl = imageUrl, Footer = new EmbedFooter { Text = msg.Timestamp.ToString(), IconUrl = artPoster.AvatarUrl }, Color = new DiscordColor("#" + Config.EmbedColor), }; await Client.SendMessageAsync(EchoChannel, "", false, ebImage); // add the messageid to the cache AddToCache(msg.Id); break; } } catch (Exception ex) { new LogWriter($"{ex.Message}\n{ex.StackTrace}"); Client.DebugLogger.LogMessage(LogLevel.Error, "Bot", ex.Message, DateTime.Now); } new LogWriter($"Messaged echoed.\n{msg.Content}\n{imageUrl}"); Client.DebugLogger.LogMessage(LogLevel.Info, "Bot", $"Messaged echoed.\n{msg.Content}\n{imageUrl}", DateTime.Now); } catch (Exception exc) { new LogWriter($"{exc.Message}\n{exc.StackTrace}"); Client.DebugLogger.LogMessage(LogLevel.Error, "Bot", exc.Message, DateTime.Now); } }
/// <summary> /// Adds a message id to cache and saves the cache /// </summary> /// <param name="msgId"></param> private void AddToCache(ulong msgId) { EchoedCache.Add(msgId); SaveCache(); }