diff --git a/DiscordBot/Extensions/StringExtensions.cs b/DiscordBot/Extensions/StringExtensions.cs index 6f2153b4..1f0ef267 100644 --- a/DiscordBot/Extensions/StringExtensions.cs +++ b/DiscordBot/Extensions/StringExtensions.cs @@ -161,4 +161,18 @@ public static string ToCommaList(this string[] nouns, string conj=null) } return sb.ToString(); } + + public static string ToBold(this string text) + { + return $"**{text}**"; + } + + public static string[] ToBoldArray(this string[] texts) + { + var bolds = new string[texts.Length]; + for (int i = 0; i < texts.Length; i++) + bolds[i] = texts[i].ToBold(); + return bolds; + } + } diff --git a/DiscordBot/Modules/ModerationModule.cs b/DiscordBot/Modules/ModerationModule.cs index aecfb26e..48de2952 100644 --- a/DiscordBot/Modules/ModerationModule.cs +++ b/DiscordBot/Modules/ModerationModule.cs @@ -125,7 +125,8 @@ await botCommandChannel.SendMessageAsync( await UserService.MutedUsers.AwaitCooldown(u.Id); await UnmuteUser(user, true); - reply?.DeleteAsync(); + if (reply != null) + await reply.DeleteAsync(); } [Command("Unmute")] @@ -493,7 +494,7 @@ public async Task ChannelTags(ulong channelId) .WithColor(Color.Blue) .Build(); - Context.Message.DeleteAsync(); + await Context.Message.DeleteAsync(); await ReplyAsync(embed: embed).DeleteAfterSeconds(seconds: 60); } @@ -511,4 +512,4 @@ public async Task ModerationHelp() } } #endregion -} \ No newline at end of file +} diff --git a/DiscordBot/Services/UserService.cs b/DiscordBot/Services/UserService.cs index a5b90277..2d5ccea6 100644 --- a/DiscordBot/Services/UserService.cs +++ b/DiscordBot/Services/UserService.cs @@ -96,11 +96,12 @@ Init thanks */ var sbThanks = new StringBuilder(); var thx = userSettings.Thanks; - sbThanks.Append("(?i)\\b("); - foreach (var t in thx) sbThanks.Append(t).Append("|"); - + sbThanks.Append(@"(?i)(? cachedMessage, SocketM { if (_canEditThanks.Contains(messageParam.Id)) await Thanks(messageParam); } - + public async Task Thanks(SocketMessage messageParam) { //Get guild id @@ -449,9 +451,11 @@ public async Task Thanks(SocketMessage messageParam) var match = Regex.Match(messageParam.Content, _thanksRegex); if (!match.Success) return; - var mentions = messageParam.MentionedUsers; - mentions = mentions.Distinct().ToList(); + var userId = messageParam.Author.Id; + var mentions = messageParam.MentionedUsers; + mentions = mentions.Distinct().Where(who => !who.IsBot && who.Id != userId).ToList(); + const int defaultDelTime = 120; if (mentions.Count > 0) { @@ -473,46 +477,22 @@ await messageParam.Channel.SendMessageAsync( return; } - var mentionedSelf = false; - var mentionedBot = false; var sb = new StringBuilder(); - sb.Append("**").Append(messageParam.Author.GetUserPreferredName()).Append("** gave karma to **"); - foreach (var user in mentions) - { - if (user.IsBot) - { - mentionedBot = true; - continue; - } - - if (user.Id == userId) - { - mentionedSelf = true; - continue; - } - - await _databaseService.Query.IncrementKarma(user.Id.ToString()); - sb.Append(user.GetUserPreferredName()).Append("**, **"); - } + sb.Append(messageParam.Author.GetUserPreferredName().ToBold()); + sb.Append(" gave karma to "); + sb.Append(mentions.ToArray().ToUserPreferredNameArray().ToBoldArray().ToCommaList()); + foreach (var mention in mentions) + await _databaseService.Query.IncrementKarma(mention.Id.ToString()); - // Even if a user gives multiple karma in one message, we only add one. + // Even if a user gives multiple karma in one message, we only give one credit. var authorKarmaGiven = await _databaseService.Query.GetKarmaGiven(messageParam.Author.Id.ToString()); await _databaseService.Query.UpdateKarmaGiven(messageParam.Author.Id.ToString(), authorKarmaGiven + 1); - sb.Length -= 4; //Removes last instance of appended comma/startbold without convoluted tracking - //sb.Append("**"); // Already appended an endbold sb.Append("."); - if (mentionedSelf) - await messageParam.Channel.SendMessageAsync( - $"{messageParam.Author.Mention} you can't give karma to yourself.").DeleteAfterTime(defaultDelTime); _canEditThanks.Remove(messageParam.Id); - - //Don't give karma cooldown if user only mentioned himself or the bot or both - if ((mentionedSelf || mentionedBot) && mentions.Count == 1 || - mentionedBot && mentionedSelf && mentions.Count == 2) - return; _thanksCooldown.AddCooldown(userId, _thanksCooldownTime); + await messageParam.Channel.SendMessageAsync(sb.ToString()); await _loggingService.LogChannelAndFile(sb + " in channel " + messageParam.Channel.Name); } diff --git a/DiscordBot/Settings/Deserialized/Settings.cs b/DiscordBot/Settings/Deserialized/Settings.cs index e188c1c0..f4868bff 100644 --- a/DiscordBot/Settings/Deserialized/Settings.cs +++ b/DiscordBot/Settings/Deserialized/Settings.cs @@ -106,6 +106,8 @@ public class BotSettings public ulong SubsNewsRoleId { get; set; } public ulong PublisherRoleId { get; set; } public ulong ModeratorRoleId { get; set; } + public ulong TipsUserRoleId { get; set; } // e.g., Helpers + public ulong TipsAuthorRoleId { get; set; } // e.g., Moderators #endregion // User Roles diff --git a/DiscordBot/Utils/WebUtil.cs b/DiscordBot/Utils/WebUtil.cs index b521eb86..80cd6591 100644 --- a/DiscordBot/Utils/WebUtil.cs +++ b/DiscordBot/Utils/WebUtil.cs @@ -38,7 +38,7 @@ public static async Task GetHtmlDocument(string url) doc.LoadHtml(html); return doc; } - catch (Exception e) + catch (Exception) { return null; } @@ -55,7 +55,7 @@ public static async Task GetHtmlNode(string url, string xpath) var doc = await GetHtmlDocument(url); return doc.DocumentNode.SelectSingleNode(xpath); } - catch (Exception e) + catch (Exception) { return null; } @@ -71,7 +71,7 @@ public static async Task GetHtmlNodes(string url, string xpa var doc = await GetHtmlDocument(url); return doc.DocumentNode.SelectNodes(xpath); } - catch (Exception e) + catch (Exception) { return null; } @@ -87,7 +87,7 @@ public static async Task GetHtmlNodeInnerText(string url, string xpath) var node = await GetHtmlNode(url, xpath); return WebUtility.HtmlDecode(node?.InnerText); } - catch (Exception e) + catch (Exception) { return string.Empty; } @@ -147,4 +147,4 @@ public static async Task GetObjectFromJson(string url) return (false, default); } } -} \ No newline at end of file +}