Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions DiscordBot/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@

public static string GetSha256(this string input)
{
var hash = new SHA256CryptoServiceProvider();

Check warning on line 106 in DiscordBot/Extensions/StringExtensions.cs

View workflow job for this annotation

GitHub Actions / Build & Test

'SHA256CryptoServiceProvider' is obsolete: 'Derived cryptographic types are obsolete. Use the Create method on the base type instead.'
// Convert the input string to a byte array and compute the hash.
var data = hash.ComputeHash(Encoding.UTF8.GetBytes(input));

Expand Down Expand Up @@ -161,4 +161,18 @@
}
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;
}

}
7 changes: 4 additions & 3 deletions DiscordBot/Modules/ModerationModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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);
}

Expand All @@ -511,4 +512,4 @@ public async Task ModerationHelp()
}
}
#endregion
}
}
58 changes: 19 additions & 39 deletions DiscordBot/Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,12 @@
*/
var sbThanks = new StringBuilder();
var thx = userSettings.Thanks;
sbThanks.Append("(?i)\\b(");
foreach (var t in thx) sbThanks.Append(t).Append("|");

sbThanks.Append(@"(?i)(?<!\bno\s*)\b(");
Comment thread
hariedo marked this conversation as resolved.
foreach (var t in thx)
sbThanks.Append(t).Append('|');
sbThanks.Length--; //Efficiently remove the final pipe that gets added in final loop, simplifying loop
sbThanks.Append(")\\b");
sbThanks.Append(@")\b");

_thanksRegex = sbThanks.ToString();
_thanksCooldownTime = userSettings.ThanksCooldown;
_thanksMinJoinTime = userSettings.ThanksMinJoinTime;
Expand Down Expand Up @@ -206,7 +207,7 @@
_updateService.SetUserData(data);
}

public async Task UpdateXp(SocketMessage messageParam)

Check warning on line 210 in DiscordBot/Services/UserService.cs

View workflow job for this annotation

GitHub Actions / Build & Test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
if (messageParam.Author.IsBot)
return;
Expand All @@ -224,7 +225,7 @@

// Add Delay and delay action by 200ms to avoid some weird database collision?
_xpCooldown.AddCooldown(userId, waitTime);
Task.Run(async () =>

Check warning on line 228 in DiscordBot/Services/UserService.cs

View workflow job for this annotation

GitHub Actions / Build & Test

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
{
var user = await _databaseService.GetOrAddUser((SocketGuildUser)messageParam.Author);
if (user == null)
Expand Down Expand Up @@ -274,7 +275,8 @@
if (level <= 3)
return;

await messageParam.Channel.SendMessageAsync($"**{messageParam.Author.GetUserPreferredName()}** has leveled up!").DeleteAfterTime(60);
var msg = messageParam.Author.GetUserPreferredName().ToBold() + " has leveled up!";
await messageParam.Channel.SendMessageAsync(msg).DeleteAfterTime(60);
//TODO Add level up card
}

Expand Down Expand Up @@ -434,7 +436,7 @@
{
if (_canEditThanks.Contains(messageParam.Id)) await Thanks(messageParam);
}

public async Task Thanks(SocketMessage messageParam)
{
//Get guild id
Expand All @@ -449,9 +451,11 @@
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)
{
Expand All @@ -473,46 +477,22 @@
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());

Comment thread
hariedo marked this conversation as resolved.
// 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);
}
Expand Down Expand Up @@ -735,7 +715,7 @@
await _loggingService.LogAction($"{ServiceName}: Welcome service failed on first run!? This should not happen.", ExtendedLogSeverity.Error);

// Run the service again.
Task.Run(DelayedWelcomeService);

Check warning on line 718 in DiscordBot/Services/UserService.cs

View workflow job for this annotation

GitHub Actions / Build & Test

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
}
}

Expand Down
2 changes: 2 additions & 0 deletions DiscordBot/Settings/Deserialized/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions DiscordBot/Utils/WebUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static async Task<HtmlDocument> GetHtmlDocument(string url)
doc.LoadHtml(html);
return doc;
}
catch (Exception e)
catch (Exception)
{
Comment thread
hariedo marked this conversation as resolved.
return null;
}
Expand All @@ -55,7 +55,7 @@ public static async Task<HtmlNode> GetHtmlNode(string url, string xpath)
var doc = await GetHtmlDocument(url);
return doc.DocumentNode.SelectSingleNode(xpath);
}
catch (Exception e)
catch (Exception)
{
return null;
}
Expand All @@ -71,7 +71,7 @@ public static async Task<HtmlNodeCollection> GetHtmlNodes(string url, string xpa
var doc = await GetHtmlDocument(url);
return doc.DocumentNode.SelectNodes(xpath);
}
catch (Exception e)
catch (Exception)
{
return null;
}
Expand All @@ -87,7 +87,7 @@ public static async Task<string> GetHtmlNodeInnerText(string url, string xpath)
var node = await GetHtmlNode(url, xpath);
return WebUtility.HtmlDecode(node?.InnerText);
}
catch (Exception e)
catch (Exception)
{
return string.Empty;
}
Expand Down Expand Up @@ -147,4 +147,4 @@ public static async Task<T> GetObjectFromJson<T>(string url)
return (false, default);
}
}
}
}
Loading