Skip to content

Commit

Permalink
Completed bot finalization
Browse files Browse the repository at this point in the history
  • Loading branch information
CPULL committed Aug 19, 2021
1 parent 9890a93 commit c69fbf7
Show file tree
Hide file tree
Showing 17 changed files with 920 additions and 728 deletions.
30 changes: 30 additions & 0 deletions ToDo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Have command to alter the command character [NOT POSSIBLE!]
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
Have a cmd to configure how long messages should stay
Add TryCatch to all code and commands


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 |

547 changes: 302 additions & 245 deletions UPBot Code/Actions/AppreciationTracking.cs

Large diffs are not rendered by default.

68 changes: 40 additions & 28 deletions UPBot Code/Actions/MembersTracking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,73 @@ public class MembersTracking {
static DiscordChannel trackChannel = null;

public static async Task DiscordMemberRemoved(DiscordClient client, DSharpPlus.EventArgs.GuildMemberRemoveEventArgs args) {
try{
if (tracking == null) tracking = new Dictionary<ulong, DateTime>();
if (trackChannel == null) trackChannel = args.Guild.GetChannel(831186370445443104ul);

if (tracking.ContainsKey(args.Member.Id)) {
tracking.Remove(args.Member.Id);
string msg = "User " + args.Member.DisplayName + " did a kiss and go.";
await trackChannel.SendMessageAsync(msg);
UtilityFunctions.Log(msg);
Utils.Log(msg);
}
else {
string msgC = UtilityFunctions.GetEmojiSnowflakeID(EmojiEnum.KO) + " User " + args.Member.Mention + " left on " + DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + " (" + args.Guild.MemberCount + " memebrs total)";
string msgC = Utils.GetEmojiSnowflakeID(EmojiEnum.KO) + " User " + args.Member.Mention + " left on " + DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + " (" + args.Guild.MemberCount + " memebrs total)";
string msgL = "- User " + args.Member.DisplayName + " left on " + DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + " (" + args.Guild.MemberCount + " memebrs total)";
await trackChannel.SendMessageAsync(msgC);
UtilityFunctions.Log(msgL);
Utils.Log(msgL);
}
} catch (Exception ex) {
Utils.Log("Error in DiscordMemberRemoved: " + ex.Message);
}

await Task.Delay(10);
}

public static async Task DiscordMemberAdded(DiscordClient client, DSharpPlus.EventArgs.GuildMemberAddEventArgs args) {
try{
if (tracking == null) tracking = new Dictionary<ulong, DateTime>();
if (trackChannel == null) trackChannel = args.Guild.GetChannel(831186370445443104ul);
tracking[args.Member.Id] = DateTime.Now;
_ = SomethingAsync(args.Member.Id, args.Member.DisplayName, args.Member.Mention, args.Guild.MemberCount);
} catch (Exception ex) {
Utils.Log("Error in DiscordMemberAdded: " + ex.Message);
}
await Task.Delay(10);
}

public static async Task DiscordMemberUpdated(DiscordClient client, DSharpPlus.EventArgs.GuildMemberUpdateEventArgs args) {
if (tracking == null) tracking = new Dictionary<ulong, DateTime>();
if (trackChannel == null) trackChannel = args.Guild.GetChannel(831186370445443104ul);
try {
if (tracking == null) tracking = new Dictionary<ulong, DateTime>();
if (trackChannel == null) trackChannel = args.Guild.GetChannel(831186370445443104ul);

IReadOnlyList<DiscordRole> rolesBefore = args.RolesBefore;
IReadOnlyList<DiscordRole> rolesAfter = args.RolesAfter;
List<DiscordRole> rolesAdded = new List<DiscordRole>();
// Changed role?
foreach (DiscordRole r1 in rolesAfter) {
bool addedRole = true;
foreach (DiscordRole r2 in rolesBefore) {
if (r1.Equals(r2)) {
addedRole = false;
IReadOnlyList<DiscordRole> rolesBefore = args.RolesBefore;
IReadOnlyList<DiscordRole> rolesAfter = args.RolesAfter;
List<DiscordRole> rolesAdded = new List<DiscordRole>();
// Changed role?
foreach (DiscordRole r1 in rolesAfter) {
bool addedRole = true;
foreach (DiscordRole r2 in rolesBefore) {
if (r1.Equals(r2)) {
addedRole = false;
}
}
if (addedRole) rolesAdded.Add(r1);
}
if (addedRole) rolesAdded.Add(r1);
}
string msgC;
string msgL;
if (rolesAdded.Count > 0) {
msgC = "User " + args.Member.Mention + " has the new role" + (rolesAdded.Count > 1 ? "s:" : ":");
msgL = "User \"" + args.Member.DisplayName + "\" has the new role" + (rolesAdded.Count > 1 ? "s:" : ":");
foreach (DiscordRole r in rolesAdded) {
msgC += r.Mention;
msgL += r.Name;
string msgC;
string msgL;
if (rolesAdded.Count > 0) {
msgC = "User " + args.Member.Mention + " has the new role" + (rolesAdded.Count > 1 ? "s:" : ":");
msgL = "User \"" + args.Member.DisplayName + "\" has the new role" + (rolesAdded.Count > 1 ? "s:" : ":");
foreach (DiscordRole r in rolesAdded) {
msgC += r.Mention;
msgL += r.Name;
}
await trackChannel.SendMessageAsync(msgC);
Utils.Log(msgL);
}
await trackChannel.SendMessageAsync(msgC);
UtilityFunctions.Log(msgL);
} catch (Exception ex) {
Utils.Log("Error in DiscordMemberUpdated: " + ex.Message);
}

await Task.Delay(10);
Expand All @@ -73,10 +85,10 @@ public static async Task DiscordMemberUpdated(DiscordClient client, DSharpPlus.E
static async Task SomethingAsync(ulong id, string name, string mention, int numMembers) {
await Task.Delay(25000);
if (tracking.ContainsKey(id)) {
string msgC = UtilityFunctions.GetEmojiSnowflakeID(EmojiEnum.OK) + " User " + mention + " joined on " + DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + " (" + numMembers + " memebrs total)";
string msgC = Utils.GetEmojiSnowflakeID(EmojiEnum.OK) + " User " + mention + " joined on " + DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + " (" + numMembers + " memebrs total)";
string msgL = "+ User " + name + " joined on " + DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + " (" + numMembers + " memebrs total)";
await trackChannel.SendMessageAsync(msgC);
UtilityFunctions.Log(msgL);
Utils.Log(msgL);
tracking.Remove(id);
}
}
Expand Down
180 changes: 94 additions & 86 deletions UPBot Code/Commands/BannedWords.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class BannedWords : BaseCommandModule {

public static void Init() {
bannedWords = new List<BannedWord>();
string path = UtilityFunctions.ConstructPath(directoryName, "BannedWords", ".txt");
string path = Utils.ConstructPath(directoryName, "BannedWords", ".txt");
if (!File.Exists(path)) return;
string[] all = File.ReadAllLines(path);
foreach (string line in all) {
Expand All @@ -37,110 +37,115 @@ public static void Init() {
[Description("To handle banned words. It can be done only by Mods and Helpers")]
[RequireRoles(RoleCheckMode.Any, "Helper", "Mod", "Owner")] // Restrict this command to "Helper", "Mod" and "Owner" roles only
public async Task BannedWordsCommand(CommandContext ctx) {
UtilityFunctions.LogUserCommand(ctx);
Utils.LogUserCommand(ctx);
await ctx.Channel.SendMessageAsync("Use the commands `list`, `add`, and `remove` to handle banned words.");
}

[Command("bannedwords")]
[Description("To handle banned words. It can be done only by Mods and Helpers")]
[RequireRoles(RoleCheckMode.Any, "Helper", "Mod", "Owner")] // Restrict this command to "Helper", "Mod" and "Owner" roles only
public async Task BannedWordsCommand(CommandContext ctx, [Description("Command to use (list, add, remove)")] string command) {
UtilityFunctions.LogUserCommand(ctx);
Utils.LogUserCommand(ctx);
await HandleListOfBannedWords(ctx, command);
}

[Command("bannedwords")]
[Description("To handle banned words. It can be done only by Mods and Helpers")]
[RequireRoles(RoleCheckMode.Any, "Helper", "Mod", "Owner")] // Restrict this command to "Helper", "Mod" and "Owner" roles only
public async Task BannedWordsCommand(CommandContext ctx, [Description("Command to use (list, add, remove)")] string command, [Description("The word to add or remove (not used when listing)")] string word) {
UtilityFunctions.LogUserCommand(ctx);
Task<DiscordMessage> msg = await HandleAddRemoveOfBannedWords(ctx, command, word);
await Task.Delay(5000);
await msg.Result.DeleteAsync();
await Task.Delay(1000);
await ctx.Message.DeleteAsync();

Utils.LogUserCommand(ctx);
Task<DiscordMessage> msg = HandleAddRemoveOfBannedWords(ctx, command, word).Result;
Utils.DeleteDelayed(30, msg.Result).Wait();
await Utils.DeleteDelayed(10, ctx.Message);
}

private async Task<Task<DiscordMessage>> HandleListOfBannedWords(CommandContext ctx, string command) {
if (command.ToLowerInvariant() != "list") return ctx.Channel.SendMessageAsync("Use: list, add, or remove.");
else if (bannedWords == null || bannedWords.Count == 0) return ctx.Channel.SendMessageAsync("There are no banned words I am aware of.");
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") + ")";
if (i < bannedWords.Count - 1) message += ",\n";
try {
if (command.ToLowerInvariant() != "list") return ctx.Channel.SendMessageAsync("Use: list, add, or remove.");
else if (bannedWords == null || bannedWords.Count == 0) return ctx.Channel.SendMessageAsync("There are no banned words I am aware of.");
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") + ")";
if (i < bannedWords.Count - 1) message += ",\n";
}
await Task.Delay(10);
return ctx.Channel.SendMessageAsync(message);
}
await Task.Delay(10);
return ctx.Channel.SendMessageAsync(message);
} catch (Exception ex) {
return ctx.RespondAsync(Utils.GenerateErrorAnswer("BannedWords.List", ex));
}
}

private async Task<Task<DiscordMessage>> HandleAddRemoveOfBannedWords(CommandContext ctx, string command, string word) {
await Task.Delay(10);
if (command.ToLowerInvariant() == "add") {
word = word.Trim(' ', '\r', '\n').ToLowerInvariant();
if (string.IsNullOrWhiteSpace(word) || !valid.IsMatch(word)) return ctx.Channel.SendMessageAsync("Not a valid word");
if (bannedWords == null) bannedWords = new List<BannedWord>();
// Do we have it?
foreach (BannedWord bw in bannedWords) {
if (bw.word.Equals(word)) {
await ctx.Message.CreateReactionAsync(UtilityFunctions.GetEmoji(EmojiEnum.KO));
return ctx.Channel.SendMessageAsync("The word \"" + word + "\" is already in the list.");
try {
await Task.Delay(10);
if (command.ToLowerInvariant() == "add") {
word = word.Trim(' ', '\r', '\n').ToLowerInvariant();
if (string.IsNullOrWhiteSpace(word) || !valid.IsMatch(word)) return ctx.Channel.SendMessageAsync("Not a valid word");
if (bannedWords == null) bannedWords = new List<BannedWord>();
// Do we have it?
foreach (BannedWord bw in bannedWords) {
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);
BannedWord w = new BannedWord(word, ctx.Message.Author.Id);
bannedWords.Add(w);
bannedWords.Sort((a, b) => { return a.word.CompareTo(b.word); });
SaveWord(w);

await ctx.Message.CreateReactionAsync(UtilityFunctions.GetEmoji(EmojiEnum.OK));
return ctx.Channel.SendMessageAsync("The word \"" + word + "\" has been added.");
}
else if (command.ToLowerInvariant() == "remove") {
word = word.Trim(' ', '\r', '\n').ToLowerInvariant();
if (string.IsNullOrWhiteSpace(word) || !Regex.IsMatch(word, @"^[a-zA-Z0-9]+$")) return ctx.Channel.SendMessageAsync("Not a valid word");
if (bannedWords == null) bannedWords = new List<BannedWord>();
// Do we have it?
BannedWord found = null;
foreach (BannedWord bw in bannedWords) {
if (bw.word.Equals(word)) {
found = bw;
break;
}
await ctx.Message.CreateReactionAsync(Utils.GetEmoji(EmojiEnum.OK));
return ctx.Channel.SendMessageAsync("The word \"" + word + "\" has been added.");
}
if (found == null) {
await ctx.Message.CreateReactionAsync(UtilityFunctions.GetEmoji(EmojiEnum.KO));
return ctx.Channel.SendMessageAsync("The word \"" + word + "\" is not in the list.");
else if (command.ToLowerInvariant() == "remove") {
word = word.Trim(' ', '\r', '\n').ToLowerInvariant();
if (string.IsNullOrWhiteSpace(word) || !Regex.IsMatch(word, @"^[a-zA-Z0-9]+$")) return ctx.Channel.SendMessageAsync("Not a valid word");
if (bannedWords == null) bannedWords = new List<BannedWord>();
// Do we have it?
BannedWord found = null;
foreach (BannedWord bw in bannedWords) {
if (bw.word.Equals(word)) {
found = bw;
break;
}
}
if (found == null) {
await ctx.Message.CreateReactionAsync(Utils.GetEmoji(EmojiEnum.KO));
return ctx.Channel.SendMessageAsync("The word \"" + word + "\" is not in the list.");
}
bannedWords.Remove(found);
SaveList();
await ctx.Message.CreateReactionAsync(Utils.GetEmoji(EmojiEnum.OK));
return ctx.Channel.SendMessageAsync("The word \"" + word + "\" has been removed.");
}
bannedWords.Remove(found);
SaveList();
await ctx.Message.CreateReactionAsync(UtilityFunctions.GetEmoji(EmojiEnum.OK));
return ctx.Channel.SendMessageAsync("The word \"" + word + "\" has been removed.");
else return ctx.Channel.SendMessageAsync("Use: add or remove and then the word.");
} catch (Exception ex) {
return ctx.RespondAsync(Utils.GenerateErrorAnswer("BannedWords.Handle", ex));
}
else return ctx.Channel.SendMessageAsync("Use: add or remove and then the word.");
}

void SaveWord(BannedWord w) {
string path = UtilityFunctions.ConstructPath(directoryName, "BannedWords", ".txt");
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) {
UtilityFunctions.Log(e.Message);
Utils.Log(e.Message);
}
}

void SaveList() {
string path = UtilityFunctions.ConstructPath(directoryName, "BannedWords", ".txt");
string path = Utils.ConstructPath(directoryName, "BannedWords", ".txt");
if (File.Exists(path)) {
try {
File.Delete(path);
} catch (Exception e) {
UtilityFunctions.Log(e.Message);
Utils.Log(e.Message);
return;
}
}
Expand All @@ -152,7 +157,7 @@ void SaveList() {
}
}
} catch (Exception e) {
UtilityFunctions.Log(e.Message);
Utils.Log(e.Message);
}
}

Expand Down Expand Up @@ -199,33 +204,36 @@ async Task<DiscordUser> GetUserNameFromDiscord(ulong userId, CommandContext ctx)
}

internal static async Task CheckMessage(DiscordClient client, MessageCreateEventArgs args) {
// 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);
} catch (Exception) {
return;
}
foreach (DiscordRole role in member.Roles) {
if (role.Id == 831050318171078718ul /* Helper */ || role.Id == 830901743624650783ul /* Mod */ || role.Id == 830901562960117780ul /* Owner */) return;
}
// 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);
} catch (Exception) {
return;
}
foreach (DiscordRole role in member.Roles) {
if (role.Id == 831050318171078718ul /* Helper */ || role.Id == 830901743624650783ul /* Mod */ || role.Id == 830901562960117780ul /* Owner */) return;
}

string msg = args.Message.Content.ToLowerInvariant();
foreach (BannedWord w in bannedWords) {
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;

UtilityFunctions.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 Task.Delay(10000);
await warning.DeleteAsync();
return;
string msg = args.Message.Content.ToLowerInvariant();
foreach (BannedWord w in bannedWords) {
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;

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);
Utils.DeleteDelayed(10000, warning).Wait();
return;
}
} catch (Exception ex) {
await args.Message.RespondAsync(Utils.GenerateErrorAnswer("BannedWords.CheckMessage", ex));
}
}

Expand Down
Loading

0 comments on commit c69fbf7

Please sign in to comment.