Skip to content

Commit

Permalink
Merge pull request #83 from United-Programming/cpu_dev
Browse files Browse the repository at this point in the history
🎨 Completed Database migration
  • Loading branch information
CPULL authored Aug 21, 2021
2 parents c69fbf7 + cfbb4a8 commit 649237d
Show file tree
Hide file tree
Showing 22 changed files with 1,192 additions and 833 deletions.
51 changes: 26 additions & 25 deletions ToDo.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
Have command to alter the command character [NOT POSSIBLE!]
Check all command names
!Check all command names
Add wikis for each command on GitHub
Uniform all error messages with Embeds (use Duck code)
Have all messages and commands to disappear after a while (configurable) if not needed to stay
!Uniform all error messages with Embeds (use Duck code)
!Have all messages and commands to disappear after a while (configurable) if not needed to stay
Have a cmd to configure how long messages should stay
Add TryCatch to all code and commands
!Add TryCatch to all code and commands
Add "keep" option to reformat
Extend the reputation by checking "tanks and thank you" in messages and add to mentioned mebers or author of referenced message


Errors DelAnsw EmbedRes TryCatch Log@Begin
MemberTracking | | | | X | |
Appreciation | | | | X | X |
EmojiForRole | | | | X | X |
BannedWords | | X | | X | X |
newcc | | | | | X |
ccdel | | | | | X |
ccedit | | | | | X |
cceditname | | | | | X |
cclist | | | | | X |
delete | X | X | | X | X |
game | | | | | X |
bool | | | | | X |
rps | | | | | X |
helplanguage | | | X | X | X |
ping | | X | | X | X |
checklanguage | | | | X | X |
reformat | | | | X | X |
stats | | | X | X | X |
whois | | | X | X | X |
Errors DelAnsw EmbedRes TryCatch Log@Begin UseSQL
MemberTracking | | | | X | | |
Appreciation | | | | X | X | X |
EmojiForRole | | | | X | X | X |
BannedWords | | X | | X | X | X |
newcc | | | | | X | |
ccdel | | | | | X | |
ccedit | | | | | X | |
cceditname | | | | | X | |
cclist | | | | | X | |
delete | X | X | | X | X | |
game | | | | | X | |
bool | | | | | X | |
rps | | | | | X | |
helplanguage | | | X | X | X | |
ping | | X | | X | X | |
checklanguage | | | | X | X | |
reformat | | | | X | X | |
stats | | | X | X | X | |
whois | | | X | X | X | |

506 changes: 61 additions & 445 deletions UPBot Code/Actions/AppreciationTracking.cs

Large diffs are not rendered by default.

95 changes: 15 additions & 80 deletions UPBot Code/Commands/BannedWords.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,9 @@ public class BannedWords : BaseCommandModule {
private const string directoryName = "Restrictions";

public static void Init() {
bannedWords = new List<BannedWord>();
string path = Utils.ConstructPath(directoryName, "BannedWords", ".txt");
if (!File.Exists(path)) return;
string[] all = File.ReadAllLines(path);
foreach (string line in all) {
BannedWord word = new BannedWord(line);
if (word.word == null) continue;
bannedWords.Add(word);
}
bannedWords.Sort((a, b) => { return a.word.CompareTo(b.word); });
bannedWords = Database.GetAll<BannedWord>();
Utils.Log("Found " + bannedWords.Count + " banned words");
bannedWords.Sort((a, b) => { return a.Word.CompareTo(b.Word); });
}

[Command("bannedwords")]
Expand Down Expand Up @@ -66,7 +59,7 @@ private async Task<Task<DiscordMessage>> HandleListOfBannedWords(CommandContext
else {
string message = "I have " + bannedWords.Count + " banned word" + (bannedWords.Count == 1 ? "" : "s") + ":\n";
for (int i = 0; i < bannedWords.Count; i++) {
message += bannedWords[i].word + " (" + GetUserName(bannedWords[i].creator, ctx) + " " + bannedWords[i].date.ToString("yyyy/MM/dd") + ")";
message += bannedWords[i].Word + " (" + GetUserName(bannedWords[i].Creator, ctx) + " " + bannedWords[i].DateAdded.ToString("yyyy/MM/dd") + ")";
if (i < bannedWords.Count - 1) message += ",\n";
}
await Task.Delay(10);
Expand All @@ -86,15 +79,15 @@ private async Task<Task<DiscordMessage>> HandleAddRemoveOfBannedWords(CommandCon
if (bannedWords == null) bannedWords = new List<BannedWord>();
// Do we have it?
foreach (BannedWord bw in bannedWords) {
if (bw.word.Equals(word)) {
if (bw.Word.Equals(word)) {
await ctx.Message.CreateReactionAsync(Utils.GetEmoji(EmojiEnum.KO));
return ctx.Channel.SendMessageAsync("The word \"" + word + "\" is already in the list.");
}
}
BannedWord w = new BannedWord(word, ctx.Message.Author.Id);
bannedWords.Add(w);
bannedWords.Sort((a, b) => { return a.word.CompareTo(b.word); });
SaveWord(w);
bannedWords.Sort((a, b) => { return a.Word.CompareTo(b.Word); });
Database.Add(w);

await ctx.Message.CreateReactionAsync(Utils.GetEmoji(EmojiEnum.OK));
return ctx.Channel.SendMessageAsync("The word \"" + word + "\" has been added.");
Expand All @@ -106,7 +99,7 @@ private async Task<Task<DiscordMessage>> HandleAddRemoveOfBannedWords(CommandCon
// Do we have it?
BannedWord found = null;
foreach (BannedWord bw in bannedWords) {
if (bw.word.Equals(word)) {
if (bw.Word.Equals(word)) {
found = bw;
break;
}
Expand All @@ -116,7 +109,8 @@ private async Task<Task<DiscordMessage>> HandleAddRemoveOfBannedWords(CommandCon
return ctx.Channel.SendMessageAsync("The word \"" + word + "\" is not in the list.");
}
bannedWords.Remove(found);
SaveList();
Database.Delete(found);

await ctx.Message.CreateReactionAsync(Utils.GetEmoji(EmojiEnum.OK));
return ctx.Channel.SendMessageAsync("The word \"" + word + "\" has been removed.");
}
Expand All @@ -126,64 +120,6 @@ private async Task<Task<DiscordMessage>> HandleAddRemoveOfBannedWords(CommandCon
}
}

void SaveWord(BannedWord w) {
string path = Utils.ConstructPath(directoryName, "BannedWords", ".txt");
if (!File.Exists(path)) File.CreateText(path);
try {
using (StreamWriter sw = File.AppendText(path)) {
sw.Write(w.ToString());
sw.FlushAsync();
}
} catch (Exception e) {
Utils.Log(e.Message);
}
}

void SaveList() {
string path = Utils.ConstructPath(directoryName, "BannedWords", ".txt");
if (File.Exists(path)) {
try {
File.Delete(path);
} catch (Exception e) {
Utils.Log(e.Message);
return;
}
}
try {
using (StreamWriter sw = File.CreateText(path)) {
foreach (BannedWord w in bannedWords) {
sw.Write(w.ToString());
sw.FlushAsync();
}
}
} catch (Exception e) {
Utils.Log(e.Message);
}
}

class BannedWord {
public string word;
public ulong creator = 0;
public DateTime date = DateTime.MinValue;

public BannedWord(string w, ulong id) {
word = w;
creator = id;
date = DateTime.Now;
}

public BannedWord(string line) {
string[] parts = line.Trim(' ', '\r', '\n').Split('\t');
if (parts.Length != 3) return;
word = parts[0].Trim(' ', '\r', '\n', '\t').ToLowerInvariant();
ulong.TryParse(parts[1].Trim(' ', '\r', '\n', '\t'), out creator);
DateTime.TryParse(parts[2].Trim(' ', '\r', '\n', '\t'), out date);
}

public override string ToString() {
return word + "\t" + creator + "\t" + date + "\n";
}
}

Dictionary<ulong, string> knownUsers;

Expand All @@ -208,10 +144,9 @@ internal static async Task CheckMessage(DiscordClient client, MessageCreateEvent
// Who is the author? If the bot or a mod then ignore
if (args.Author.Equals(client.CurrentUser)) return;
DiscordUser user = args.Author;
DiscordGuild guild = await client.GetGuildAsync((ulong)args.Message.Channel.GuildId);
DiscordMember member;
try {
member = await guild.GetMemberAsync(user.Id);
member = await Utils.GetGuild().GetMemberAsync(user.Id);
} catch (Exception) {
return;
}
Expand All @@ -221,14 +156,14 @@ internal static async Task CheckMessage(DiscordClient client, MessageCreateEvent

string msg = args.Message.Content.ToLowerInvariant();
foreach (BannedWord w in bannedWords) {
int pos = msg.IndexOf(w.word);
int pos = msg.IndexOf(w.Word);
if (pos == -1) continue;
if (pos > 0 && letters.IsMatch(msg[pos - 1].ToString())) continue;
if (pos + w.word.Length < msg.Length && letters.IsMatch(msg[pos + w.word.Length].ToString())) continue;
if (pos + w.Word.Length < msg.Length && letters.IsMatch(msg[pos + w.Word.Length].ToString())) continue;

Utils.Log("Removed word \"" + w.word + "\" from " + user.Username + " in: " + msg);
Utils.Log("Removed word \"" + w.Word + "\" from " + user.Username + " in: " + msg);
DiscordMessage warning = await args.Message.Channel.SendMessageAsync("Moderate your language, " + user.Mention + ".");
await args.Message.DeleteAsync("Bad words: " + w.word);
await args.Message.DeleteAsync("Bad words: " + w.Word);
Utils.DeleteDelayed(10000, warning).Wait();
return;
}
Expand Down
35 changes: 0 additions & 35 deletions UPBot Code/Commands/CustomCommand.cs

This file was deleted.

Loading

0 comments on commit 649237d

Please sign in to comment.