From 090d0d2ff224c8cd6834d26cbc0d0cea58b59b9e Mon Sep 17 00:00:00 2001 From: CPU Date: Mon, 3 Jan 2022 22:06:24 +0100 Subject: [PATCH] All changes from CPU local branch --- ToDo.txt | 3 ++ UPBot Code/Actions/AppreciationTracking.cs | 47 ++++++++++++++++++---- UPBot Code/Database.cs | 16 ++++---- UPBot Code/Program.cs | 11 ++++- 4 files changed, 61 insertions(+), 16 deletions(-) diff --git a/ToDo.txt b/ToDo.txt index 174fc3a..a5f6994 100644 --- a/ToDo.txt +++ b/ToDo.txt @@ -9,6 +9,9 @@ Have a cmd to configure how long messages should stay Add wikis for each command on GitHub Check if a message to reformat has something after the ```, consider the ``` the end of the block to reformat +Keep the last poster from each channel, and use it for te "thanks" in case there is no mention + + Errors DelAnsw EmbedRes TryCatch Log@Begin UseSQL MemberTracking | | | | X | | | Appreciation | | | | X | X | X | diff --git a/UPBot Code/Actions/AppreciationTracking.cs b/UPBot Code/Actions/AppreciationTracking.cs index dac45f3..b4df34b 100644 --- a/UPBot Code/Actions/AppreciationTracking.cs +++ b/UPBot Code/Actions/AppreciationTracking.cs @@ -73,9 +73,25 @@ public async Task ShowAppreciationCommand(CommandContext ctx) { } } + + private static Dictionary LastMemberPerChannels = null; + + internal static void InitChannelList() { + IReadOnlyDictionary channels = Utils.GetGuild().Channels; + LastMemberPerChannels = new Dictionary(); + foreach (ulong cid in channels.Keys) + LastMemberPerChannels[cid] = new LastPosters(); + } + internal static Task ThanksAdded(DiscordClient sender, MessageCreateEventArgs args) { try { string msg = args.Message.Content.ToLowerInvariant(); + ulong memberid = args.Message.Author.Id; + ulong channelid = args.Message.ChannelId; + if (LastMemberPerChannels == null) InitChannelList(); + LastPosters lp = LastMemberPerChannels[channelid]; + lp.Add(memberid); + if (thanks.IsMatch(msg) || thankyou.IsMatch(msg) || thank2you.IsMatch(msg)) { // Add thanks if (thank4n.IsMatch(msg)) return Task.FromResult(0); if (GetTracking()) return Task.FromResult(0); @@ -83,14 +99,18 @@ internal static Task ThanksAdded(DiscordClient sender, MessageCreateEventArgs ar DiscordMessage theMsg = args.Message; ulong authorId = theMsg.Author.Id; if (theMsg.Reference == null && (theMsg.MentionedUsers == null || theMsg.MentionedUsers.Count == 0)) { - // Unrelated thank you, get the previous message and check - IReadOnlyList msgs = theMsg.Channel.GetMessagesBeforeAsync(theMsg.Id, 2).Result; - theMsg = null; - foreach (DiscordMessage m in msgs) - if (m.Author.Id != authorId) { - theMsg = m; - break; - } + if (lp.secondLast != 0 || lp.secondLast != 875701548301299743ul) + tracking.AlterThankYou(lp.secondLast); + else { + // Unrelated thank you, get the previous message and check /* + IReadOnlyList msgs = theMsg.Channel.GetMessagesBeforeAsync(theMsg.Id, 2).Result; + theMsg = null; + foreach (DiscordMessage m in msgs) + if (m.Author.Id != authorId) { + theMsg = m; + break; + } + } if (theMsg == null) return Task.FromResult(0); } @@ -111,7 +131,18 @@ internal static Task ThanksAdded(DiscordClient sender, MessageCreateEventArgs ar } + internal class LastPosters { + public ulong thirdLast; + public ulong secondLast; + public ulong last; + internal void Add(ulong memberid) { + if (last == memberid) return; + thirdLast = secondLast; + secondLast = last; + last = memberid; + } + } diff --git a/UPBot Code/Database.cs b/UPBot Code/Database.cs index 75ea0ae..f4cce99 100644 --- a/UPBot Code/Database.cs +++ b/UPBot Code/Database.cs @@ -11,14 +11,16 @@ public class Database { public static void InitDb() { - // Do we have the db? - if (File.Exists("Database/BotDb.db")) - connection = new SQLiteConnection("Data Source=Database/" + DbName + ".db; Version=3; Journal Mode=Off; UTF8Encoding=True;"); // Open the database - else - connection = new SQLiteConnection("Data Source=Database/" + DbName + ".db; Version=3; Journal Mode=Off; New=True; UTF8Encoding=True;"); // Create a new database - - // Open the connection try { + // Do we have the db? + if (File.Exists("Database/BotDb.db")) + connection = new SQLiteConnection("Data Source=Database/" + DbName + ".db; Version=3; Journal Mode=Off; UTF8Encoding=True;"); // Open the database + else { + if (!Directory.Exists("Database")) Directory.CreateDirectory("Database"); + connection = new SQLiteConnection("Data Source=Database/" + DbName + ".db; Version=3; Journal Mode=Off; New=True; UTF8Encoding=True;"); // Create a new database + } + + // Open the connection connection.Open(); Console.WriteLine("DB connection open"); } catch (Exception ex) { diff --git a/UPBot Code/Program.cs b/UPBot Code/Program.cs index 0a8f28c..3844b60 100644 --- a/UPBot Code/Program.cs +++ b/UPBot Code/Program.cs @@ -1,9 +1,11 @@ using System; +using System.Collections.Generic; using System.IO; using System.Reflection; using System.Threading.Tasks; using DSharpPlus; using DSharpPlus.CommandsNext; +using DSharpPlus.Entities; using DSharpPlus.Interactivity; using DSharpPlus.Interactivity.Extensions; @@ -11,6 +13,9 @@ namespace UPBot { class Program { static StreamWriter lw = null; static void Main(string[] args) { + + + lw = File.CreateText(args.Length >= 3 ? args[2] : "debug.log"); lw.WriteLine("Log Started. Woho."); lw.Flush(); @@ -92,5 +97,9 @@ static async Task MainAsync(string token, string prefix) { await Task.Delay(-1); } + + } -} \ No newline at end of file + +} +