Skip to content

Commit

Permalink
Merge pull request #88 from United-Programming/cpu_dev
Browse files Browse the repository at this point in the history
All changes from CPU local branch
  • Loading branch information
CPULL authored Jan 3, 2022
2 parents ac67629 + 090d0d2 commit f624493
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 16 deletions.
3 changes: 3 additions & 0 deletions ToDo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
47 changes: 39 additions & 8 deletions UPBot Code/Actions/AppreciationTracking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,44 @@ public async Task ShowAppreciationCommand(CommandContext ctx) {
}
}


private static Dictionary<ulong, LastPosters> LastMemberPerChannels = null;

internal static void InitChannelList() {
IReadOnlyDictionary<ulong, DiscordChannel> channels = Utils.GetGuild().Channels;
LastMemberPerChannels = new Dictionary<ulong, LastPosters>();
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);

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<DiscordMessage> 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<DiscordMessage> 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);
}

Expand All @@ -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;
}
}



Expand Down
16 changes: 9 additions & 7 deletions UPBot Code/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 10 additions & 1 deletion UPBot Code/Program.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
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;

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();
Expand Down Expand Up @@ -92,5 +97,9 @@ static async Task MainAsync(string token, string prefix) {
await Task.Delay(-1);
}



}
}

}

0 comments on commit f624493

Please sign in to comment.