Skip to content

Commit

Permalink
Started MongoDB migration, numerous other fixes/tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Vauff committed Jul 10, 2019
1 parent c73f367 commit 760427c
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 113 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>3.10.2</version>
</dependency>
</dependencies>
<version>r8</version>
<version>r9</version>
</project>
50 changes: 19 additions & 31 deletions src/com/vauff/maunzdiscord/commands/Blacklist.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
import discord4j.core.object.entity.PrivateChannel;
import discord4j.core.object.entity.User;
import discord4j.core.object.util.Snowflake;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.json.JSONObject;
import org.bson.Document;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.mongodb.client.model.Filters.eq;

public class Blacklist extends AbstractCommand<MessageCreateEvent>
{
Expand All @@ -37,12 +38,11 @@ public void exe(MessageCreateEvent event, MessageChannel channel, User author) t
{
if (args.length != 1)
{
File file = new File(Util.getJarLocation() + "data/guilds/" + event.getGuild().block().getId().asString() + ".json");
JSONObject json = new JSONObject(Util.getFileContents(file));
List<String> blacklist = Main.mongoDatabase.getCollection("guilds").find(eq("guildId", event.getGuild().block().getId().asLong())).first().getList("blacklist", String.class);

if (args[1].equalsIgnoreCase("list"))
{
if (json.getJSONArray("blacklist").length() != 0)
if (blacklist.size() != 0)
{
if (args.length == 2 || NumberUtils.isCreatable(args[2]))
{
Expand All @@ -57,11 +57,10 @@ public void exe(MessageCreateEvent event, MessageChannel channel, User author) t
page = Integer.parseInt(args[2]);
}

ArrayList<String> blacklistArray = new ArrayList<>();
ArrayList<String> blacklistFormatted = new ArrayList<>();

for (int i = 0; i < json.getJSONArray("blacklist").length(); i++)
for (String entry : blacklist)
{
String entry = json.getJSONArray("blacklist").getString(i);
String channelSelector;
String command = "\\*" + entry.split(":")[1];

Expand All @@ -79,10 +78,10 @@ public void exe(MessageCreateEvent event, MessageChannel channel, User author) t
command = "All Commands";
}

blacklistArray.add(channelSelector + " **|** " + command);
blacklistFormatted.add(channelSelector + " **|** " + command);
}

Message m = Util.buildPage(blacklistArray, "Blacklisted Channels/Commands", 10, page, false, false, channel, author);
Message m = Util.buildPage(blacklistFormatted, "Blacklisted Channels/Commands", 10, page, false, false, channel, author);

listMessages.put(author.getId(), m.getId());
waitForReaction(m.getId(), author.getId());
Expand Down Expand Up @@ -127,10 +126,8 @@ else if (args.length == 2)
{
boolean blacklisted = false;

for (int i = 0; i < json.getJSONArray("blacklist").length(); i++)
for (String entry : blacklist)
{
String entry = json.getJSONArray("blacklist").getString(i);

if (entry.equals(channel.getId().asString() + ":" + input))
{
blacklisted = true;
Expand All @@ -144,8 +141,7 @@ else if (args.length == 2)
Util.msg(channel, author, "Removing the ***" + input + "** command from this guilds blacklist for <#" + channel.getId().asString() + ">!");
}

json.getJSONArray("blacklist").remove(i);
FileUtils.writeStringToFile(file, json.toString(2), "UTF-8");
Main.mongoDatabase.getCollection("guilds").updateOne(eq("guildId", event.getGuild().block().getId().asLong()), new Document("$pull", new Document("blacklist", channel.getId().asString() + ":" + input)));
break;
}
}
Expand All @@ -161,8 +157,7 @@ else if (args.length == 2)
Util.msg(channel, author, "Adding the ***" + input + "** command to this guilds blacklist for <#" + channel.getId().asString() + ">!");
}

json.getJSONArray("blacklist").put(channel.getId().asString() + ":" + input);
FileUtils.writeStringToFile(file, json.toString(2), "UTF-8");
Main.mongoDatabase.getCollection("guilds").updateOne(eq("guildId", event.getGuild().block().getId().asLong()), new Document("$push", new Document("blacklist", channel.getId().asString() + ":" + input)));
}
}
else
Expand Down Expand Up @@ -223,10 +218,8 @@ else if (args[1].equalsIgnoreCase("all"))
{
boolean blacklisted = false;

for (int i = 0; i < json.getJSONArray("blacklist").length(); i++)
for (String entry : blacklist)
{
String entry = json.getJSONArray("blacklist").getString(i);

if (entry.equals(location + ":" + input))
{
blacklisted = true;
Expand Down Expand Up @@ -254,8 +247,7 @@ else if (args[1].equalsIgnoreCase("all"))
}
}

json.getJSONArray("blacklist").remove(i);
FileUtils.writeStringToFile(file, json.toString(2), "UTF-8");
Main.mongoDatabase.getCollection("guilds").updateOne(eq("guildId", event.getGuild().block().getId().asLong()), new Document("$pull", new Document("blacklist", location + ":" + input)));
break;
}
}
Expand Down Expand Up @@ -285,8 +277,7 @@ else if (args[1].equalsIgnoreCase("all"))
}
}

json.getJSONArray("blacklist").put(location + ":" + input);
FileUtils.writeStringToFile(file, json.toString(2), "UTF-8");
Main.mongoDatabase.getCollection("guilds").updateOne(eq("guildId", event.getGuild().block().getId().asLong()), new Document("$push", new Document("blacklist", location + ":" + input)));
}
}
else
Expand Down Expand Up @@ -327,16 +318,14 @@ public void onReactionAdd(ReactionAddEvent event, Message message) throws Except
{
if (listMessages.containsKey(event.getUser().block().getId()) && message.getId().equals(listMessages.get(event.getUser().block().getId())))
{
File file = new File(Util.getJarLocation() + "data/guilds/" + event.getGuild().block().getId().asString() + ".json");
JSONObject json = new JSONObject(Util.getFileContents(file));
List<String> blacklist = Main.mongoDatabase.getCollection("guilds").find(eq("guildId", event.getGuild().block().getId().asLong())).first().getList("blacklist", String.class);

if (event.getEmoji().asUnicodeEmoji().get().getRaw().equals("▶"))
{
ArrayList<String> blacklistArray = new ArrayList<>();

for (int i = 0; i < json.getJSONArray("blacklist").length(); i++)
for (String entry : blacklist)
{
String entry = json.getJSONArray("blacklist").getString(i);
String channel;
String command = "\\*" + entry.split(":")[1];

Expand Down Expand Up @@ -368,9 +357,8 @@ else if (event.getEmoji().asUnicodeEmoji().get().getRaw().equals("◀"))
{
ArrayList<String> blacklistArray = new ArrayList<>();

for (int i = 0; i < json.getJSONArray("blacklist").length(); i++)
for (String entry : blacklist)
{
String entry = json.getJSONArray("blacklist").getString(i);
String channel;
String command = "\\*" + entry.split(":")[1];

Expand Down
40 changes: 20 additions & 20 deletions src/com/vauff/maunzdiscord/commands/Disable.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.vauff.maunzdiscord.commands;

import com.vauff.maunzdiscord.core.AbstractCommand;
import com.vauff.maunzdiscord.core.Main;
import com.vauff.maunzdiscord.core.Util;
import discord4j.core.event.domain.message.MessageCreateEvent;
import discord4j.core.event.domain.message.ReactionAddEvent;
Expand All @@ -10,6 +11,7 @@
import discord4j.core.object.entity.User;
import discord4j.core.object.util.Snowflake;
import org.apache.commons.io.FileUtils;
import org.bson.Document;
import org.json.JSONObject;

import java.io.File;
Expand All @@ -18,6 +20,8 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import static com.mongodb.client.model.Filters.eq;

public class Disable extends AbstractCommand<MessageCreateEvent>
{
private static HashMap<Snowflake, Snowflake> menuMessages = new HashMap<>();
Expand All @@ -27,8 +31,7 @@ public void exe(MessageCreateEvent event, MessageChannel channel, User author) t
{
if (!(channel instanceof PrivateChannel) && Util.hasPermission(author, event.getGuild().block()))
{
File guildFile = new File(Util.getJarLocation() + "data/guilds/" + event.getGuild().block().getId().asString() + ".json");
JSONObject guildJson = new JSONObject(Util.getFileContents(guildFile));
boolean guildEnabled = Main.mongoDatabase.getCollection("guilds").find(eq("guildId", event.getGuild().block().getId().asLong())).first().getBoolean("enabled");

if (Util.hasPermission(author))
{
Expand All @@ -48,11 +51,10 @@ public void exe(MessageCreateEvent event, MessageChannel channel, User author) t
}
else
{
if (guildJson.getBoolean("enabled"))
if (guildEnabled)
{
Util.msg(channel, author, "Maunz is now disabled in this guild");
guildJson.put("enabled", false);
FileUtils.writeStringToFile(guildFile, guildJson.toString(2), "UTF-8");
Main.mongoDatabase.getCollection("guilds").updateOne(eq("guildId", event.getGuild().block().getId().asLong()), new Document("$set", new Document("enabled", false)));
}
else
{
Expand All @@ -62,14 +64,14 @@ public void exe(MessageCreateEvent event, MessageChannel channel, User author) t
}
else if (channel instanceof PrivateChannel && Util.hasPermission(author))
{
File botFile = new File(Util.getJarLocation() + "config.json");
JSONObject botJson = new JSONObject(Util.getFileContents(botFile));
File file = new File(Util.getJarLocation() + "config.json");
JSONObject json = new JSONObject(Util.getFileContents(file));

if (botJson.getBoolean("enabled"))
if (json.getBoolean("enabled"))
{
Util.msg(channel, author, "Maunz is now disabled globally");
botJson.put("enabled", false);
FileUtils.writeStringToFile(botFile, botJson.toString(2), "UTF-8");
json.put("enabled", false);
FileUtils.writeStringToFile(file, json.toString(4), "UTF-8");
}
else
{
Expand All @@ -93,18 +95,17 @@ public void onReactionAdd(ReactionAddEvent event, Message message) throws Except
{
if (menuMessages.containsKey(event.getUser().block().getId()) && message.getId().equals(menuMessages.get(event.getUser().block().getId())))
{
File botFile = new File(Util.getJarLocation() + "config.json");
File guildFile = new File(Util.getJarLocation() + "data/guilds/" + event.getGuild().block().getId().asString() + ".json");
JSONObject botJson = new JSONObject(Util.getFileContents(botFile));
JSONObject guildJson = new JSONObject(Util.getFileContents(guildFile));
File file = new File(Util.getJarLocation() + "config.json");
JSONObject json = new JSONObject(Util.getFileContents(file));
boolean guildEnabled = Main.mongoDatabase.getCollection("guilds").find(eq("guildId", event.getGuild().block().getId().asLong())).first().getBoolean("enabled");

if (event.getEmoji().asUnicodeEmoji().get().getRaw().equals("1⃣"))
{
if (botJson.getBoolean("enabled"))
if (json.getBoolean("enabled"))
{
Util.msg(event.getChannel().block(), event.getUser().block(), "Maunz is now disabled globally");
botJson.put("enabled", false);
FileUtils.writeStringToFile(botFile, botJson.toString(2), "UTF-8");
json.put("enabled", false);
FileUtils.writeStringToFile(file, json.toString(4), "UTF-8");
}
else
{
Expand All @@ -113,11 +114,10 @@ public void onReactionAdd(ReactionAddEvent event, Message message) throws Except
}
else if (event.getEmoji().asUnicodeEmoji().get().getRaw().equals("2⃣"))
{
if (guildJson.getBoolean("enabled"))
if (guildEnabled)
{
Util.msg(event.getChannel().block(), event.getUser().block(), "Maunz is now disabled in this guild");
guildJson.put("enabled", false);
FileUtils.writeStringToFile(guildFile, guildJson.toString(2), "UTF-8");
Main.mongoDatabase.getCollection("guilds").updateOne(eq("guildId", event.getGuild().block().getId().asLong()), new Document("$set", new Document("enabled", false)));
}
else
{
Expand Down
40 changes: 20 additions & 20 deletions src/com/vauff/maunzdiscord/commands/Enable.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.vauff.maunzdiscord.commands;

import com.vauff.maunzdiscord.core.AbstractCommand;
import com.vauff.maunzdiscord.core.Main;
import com.vauff.maunzdiscord.core.Util;
import discord4j.core.event.domain.message.MessageCreateEvent;
import discord4j.core.event.domain.message.ReactionAddEvent;
Expand All @@ -10,6 +11,7 @@
import discord4j.core.object.entity.User;
import discord4j.core.object.util.Snowflake;
import org.apache.commons.io.FileUtils;
import org.bson.Document;
import org.json.JSONObject;

import java.io.File;
Expand All @@ -18,6 +20,8 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import static com.mongodb.client.model.Filters.eq;

public class Enable extends AbstractCommand<MessageCreateEvent>
{
private static HashMap<Snowflake, Snowflake> menuMessages = new HashMap<>();
Expand All @@ -27,8 +31,7 @@ public void exe(MessageCreateEvent event, MessageChannel channel, User author) t
{
if (!(channel instanceof PrivateChannel) && Util.hasPermission(author, event.getGuild().block()))
{
File guildFile = new File(Util.getJarLocation() + "data/guilds/" + event.getGuild().block().getId().asString() + ".json");
JSONObject guildJson = new JSONObject(Util.getFileContents(guildFile));
boolean guildEnabled = Main.mongoDatabase.getCollection("guilds").find(eq("guildId", event.getGuild().block().getId().asLong())).first().getBoolean("enabled");

if (Util.hasPermission(author))
{
Expand All @@ -48,11 +51,10 @@ public void exe(MessageCreateEvent event, MessageChannel channel, User author) t
}
else
{
if (!guildJson.getBoolean("enabled"))
if (!guildEnabled)
{
Util.msg(channel, author, "Maunz is now enabled in this guild");
guildJson.put("enabled", true);
FileUtils.writeStringToFile(guildFile, guildJson.toString(2), "UTF-8");
Main.mongoDatabase.getCollection("guilds").updateOne(eq("guildId", event.getGuild().block().getId().asLong()), new Document("$set", new Document("enabled", true)));
}
else
{
Expand All @@ -62,14 +64,14 @@ public void exe(MessageCreateEvent event, MessageChannel channel, User author) t
}
else if (channel instanceof PrivateChannel && Util.hasPermission(author))
{
File botFile = new File(Util.getJarLocation() + "config.json");
JSONObject botJson = new JSONObject(Util.getFileContents(botFile));
File file = new File(Util.getJarLocation() + "config.json");
JSONObject json = new JSONObject(Util.getFileContents(file));

if (!botJson.getBoolean("enabled"))
if (!json.getBoolean("enabled"))
{
Util.msg(channel, author, "Maunz is now enabled globally");
botJson.put("enabled", true);
FileUtils.writeStringToFile(botFile, botJson.toString(2), "UTF-8");
json.put("enabled", true);
FileUtils.writeStringToFile(file, json.toString(4), "UTF-8");
}
else
{
Expand All @@ -93,18 +95,17 @@ public void onReactionAdd(ReactionAddEvent event, Message message) throws Except
{
if (menuMessages.containsKey(event.getUser().block().getId()) && message.getId().equals(menuMessages.get(event.getUser().block().getId())))
{
File botFile = new File(Util.getJarLocation() + "config.json");
File guildFile = new File(Util.getJarLocation() + "data/guilds/" + event.getGuild().block().getId().asString() + ".json");
JSONObject botJson = new JSONObject(Util.getFileContents(botFile));
JSONObject guildJson = new JSONObject(Util.getFileContents(guildFile));
File file = new File(Util.getJarLocation() + "config.json");
JSONObject json = new JSONObject(Util.getFileContents(file));
boolean guildEnabled = Main.mongoDatabase.getCollection("guilds").find(eq("guildId", event.getGuild().block().getId().asLong())).first().getBoolean("enabled");

if (event.getEmoji().asUnicodeEmoji().get().getRaw().equals("1⃣"))
{
if (!botJson.getBoolean("enabled"))
if (!json.getBoolean("enabled"))
{
Util.msg(event.getChannel().block(), event.getUser().block(), "Maunz is now enabled globally");
botJson.put("enabled", true);
FileUtils.writeStringToFile(botFile, botJson.toString(2), "UTF-8");
json.put("enabled", true);
FileUtils.writeStringToFile(file, json.toString(4), "UTF-8");
}
else
{
Expand All @@ -113,11 +114,10 @@ public void onReactionAdd(ReactionAddEvent event, Message message) throws Except
}
else if (event.getEmoji().asUnicodeEmoji().get().getRaw().equals("2⃣"))
{
if (!guildJson.getBoolean("enabled"))
if (!guildEnabled)
{
Util.msg(event.getChannel().block(), event.getUser().block(), "Maunz is now enabled in this guild");
guildJson.put("enabled", true);
FileUtils.writeStringToFile(guildFile, guildJson.toString(2), "UTF-8");
Main.mongoDatabase.getCollection("guilds").updateOne(eq("guildId", event.getGuild().block().getId().asLong()), new Document("$set", new Document("enabled", true)));
}
else
{
Expand Down
Loading

0 comments on commit 760427c

Please sign in to comment.