Remove old classes, utilities, and listeners

This commit is contained in:
Aleksei 2020-07-23 14:15:24 -04:00
parent e7fd553c4d
commit 3769eff401
8 changed files with 0 additions and 750 deletions

View File

@ -1,51 +0,0 @@
/*
* Copyright (c) 2020. Aleksei Gryczewski
* All rights reserved.
*/
package dev.salmonllama.fsbot.commands.developer;
import dev.salmonllama.fsbot.guthix.Command;
import dev.salmonllama.fsbot.guthix.CommandContext;
import dev.salmonllama.fsbot.guthix.CommandPermission;
import dev.salmonllama.fsbot.guthix.PermissionType;
import dev.salmonllama.fsbot.utilities.database.DatabaseUtilities;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
public class EvalCommand extends Command {
@Override public String name() { return "Eval"; }
@Override public String description() { return "Evaluates the given parameters"; }
@Override public String usage() { return "eval <statement>"; }
@Override public String category() { return "Developer"; }
@Override public CommandPermission permission() { return new CommandPermission(PermissionType.OWNER); }
@Override public Collection<String> aliases() { return new ArrayList<>(Arrays.asList("eval", "ev")); }
private final DatabaseUtilities db;
public EvalCommand(DatabaseUtilities db) {
this.db = db;
}
@Override
public void onCommand(CommandContext ctx) {
String statement = String.join(" ", ctx.getArgs());
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
engine.put("db", db);
try {
Object result = engine.eval(statement);
ctx.reply(result.toString());
}
catch (Exception e) {
ctx.reply(e.getMessage());
}
}
}

View File

@ -1,93 +0,0 @@
/*
* Copyright (c) 2020. Aleksei Gryczewski
* All rights reserved.
*/
package dev.salmonllama.fsbot.listeners;
import com.vdurmont.emoji.EmojiParser;
import dev.salmonllama.fsbot.config.BotConfig;
import org.javacord.api.entity.channel.Channel;
import org.javacord.api.entity.message.Message;
import org.javacord.api.entity.message.embed.EmbedBuilder;
import org.javacord.api.entity.user.User;
import org.javacord.api.event.message.reaction.ReactionAddEvent;
import org.javacord.api.listener.message.reaction.ReactionAddListener;
import dev.salmonllama.fsbot.utilities.Outfit;
import dev.salmonllama.fsbot.utilities.database.DatabaseUtilities;
import dev.salmonllama.fsbot.utilities.exceptions.DiscordError;
import dev.salmonllama.fsbot.utilities.exceptions.OutfitNotFoundException;
import java.awt.*;
public class ReactionDeleteConfirmationListener implements ReactionAddListener {
private final User author;
private final Message message;
private final Outfit outfit;
private final DatabaseUtilities db;
public ReactionDeleteConfirmationListener(User author, Message message, Outfit outfit, DatabaseUtilities db) {
this.author = author;
this.message = message;
this.outfit = outfit;
this.db = db;
}
public void onReactionAdd(ReactionAddEvent event) {
if (!event.getUser().getIdAsString().equals(author.getIdAsString())) {
return;
}
if (event.getEmoji().equalsEmoji(EmojiParser.parseToUnicode(":white_check_mark:"))) {
// Delete the message and send confirmation
String completed = "";
try {
completed = db.removeFromDatabase(outfit.getId());
}
catch(OutfitNotFoundException e) {
event.getChannel().sendMessage(new DiscordError(e.getMessage()).get().addField("Info:", "This message was sent by a reaction listener. **YOU SHOULD NOT BE SEEING THIS!**"));
}
if (Integer.parseInt(completed) > 0) {
// Successful deletion
EmbedBuilder embed = new EmbedBuilder()
.setTitle("Success!")
.setColor(Color.GREEN)
.setDescription("Outfit deleted successfully");
message.removeAllReactions();
message.edit(embed);
event.getApi().getChannelById(BotConfig.OUTFIT_LOG).map(Channel::asServerTextChannel).orElseThrow(AssertionError::new).map(channel ->
channel.sendMessage(outfit.generateInfo().setTitle(String.format("Outfit Deleted by %s", event.getUser().getDiscriminatedName())).setColor(Color.RED))
);
}
else {
// Deletion failure
EmbedBuilder embed = new EmbedBuilder()
.setTitle("Error!")
.setColor(Color.RED)
.setDescription("An error occurred and the outfit was not deleted. Did you use the correct ID?")
.setFooter(String.format("Bother %s about making these stupid things more useful.", event.getApi().getOwner().thenAccept(User::getDiscriminatedName)));
message.removeAllReactions();
message.edit(embed);
}
}
else if (event.getEmoji().equalsEmoji(EmojiParser.parseToUnicode(":octagonal_sign:"))) {
// Cancel deletion and do nothing
EmbedBuilder embed = new EmbedBuilder()
.setTitle("Oops!")
.setColor(Color.GREEN)
.setDescription("Deletion cancelled. No changes were made.");
message.removeAllReactions();
message.edit(embed);
}
event.getApi().removeListener(this);
}
}

View File

@ -1,107 +0,0 @@
/*
* Copyright (c) 2020. Aleksei Gryczewski
* All rights reserved.
*/
package dev.salmonllama.fsbot.listeners;
import com.vdurmont.emoji.EmojiParser;
import dev.salmonllama.fsbot.config.BotConfig;
import org.javacord.api.entity.message.Message;
import org.javacord.api.entity.message.embed.EmbedBuilder;
import org.javacord.api.entity.user.User;
import org.javacord.api.event.message.reaction.ReactionAddEvent;
import org.javacord.api.listener.message.reaction.ReactionAddListener;
import dev.salmonllama.fsbot.utilities.Outfit;
import dev.salmonllama.fsbot.utilities.database.DatabaseUtilities;
import dev.salmonllama.fsbot.utilities.exceptions.DiscordError;
import dev.salmonllama.fsbot.utilities.exceptions.OutfitNotFoundException;
import java.awt.*;
public class ReactionRetagConfirmationListener implements ReactionAddListener {
private User author;
private final Message message;
private final Outfit outfit;
private final DatabaseUtilities db;
private final String oldTag;
private final String newTag;
public ReactionRetagConfirmationListener(User author, Message message, Outfit outfit, DatabaseUtilities db, String oldTag, String newTag) {
this.author = author;
this.message = message;
this.outfit = outfit;
this.db = db;
this.oldTag = oldTag;
this.newTag = newTag;
}
public void onReactionAdd(ReactionAddEvent event) {
if (!event.getUser().getIdAsString().equals(this.author.getIdAsString())) {
return;
}
if (event.getEmoji().equalsEmoji(EmojiParser.parseToUnicode(":white_check_mark:"))) {
// retag the image, send a confirmation, or an error if it failed for whatever reason.
String result = "";
try {
result = db.changeOutfitTag(outfit.getId(), this.newTag);
}
catch (OutfitNotFoundException e) {
message.delete();
event.getChannel().sendMessage(new DiscordError(e.getMessage()).get().addField("Info", "This message was generated by a listener thread. YOU SHOULD NOT BE SEEING THIS ERROR!"));
}
message.removeAllReactions();
if (Integer.parseInt(result) > 0) {
// success, send confirmation
EmbedBuilder embed = new EmbedBuilder()
.setTitle("Success")
.setColor(Color.GREEN)
.setDescription("Outfit retagged successfully")
.setFooter("Check the log for more information");
message.edit(embed);
// log message with new tag and old tag
event.getApi().getChannelById(BotConfig.OUTFIT_LOG).ifPresent(channel -> {
channel.asServerTextChannel().ifPresent(chnl -> {
chnl.sendMessage(outfit.tagChangeEmbed(this.author.getDiscriminatedName(), this.oldTag, this.newTag));
});
});
}
else {
// failure, something went wrong
EmbedBuilder embed = new EmbedBuilder()
.setTitle("Error!")
.setColor(Color.RED)
.setFooter("Big oopsie");
event.getApi().getOwner().thenAcceptAsync(user -> {
embed.setDescription(String.format("Something has gone horribly wrong, contact %s", user.getDiscriminatedName()));
});
message.edit(embed);
}
}
else if (event.getEmoji().equalsEmoji(EmojiParser.parseToUnicode(":octagonal_sign:"))) {
// Cancel the image retagging.
message.removeAllReactions();
EmbedBuilder embed = new EmbedBuilder()
.setTitle("Operation cancelled!")
.setDescription("No changes made.")
.setColor(Color.GREEN)
.setFooter("boop");
message.edit(embed);
}
event.getApi().removeListener(this);
}
}

View File

@ -1,41 +0,0 @@
/*
* Copyright (c) 2020. Aleksei Gryczewski
* All rights reserved.
*/
package dev.salmonllama.fsbot.utilities;
import org.javacord.api.entity.message.embed.EmbedBuilder;
import dev.salmonllama.fsbot.utilities.database.RoleColourUtility;
import java.util.List;
import java.awt.Color;
public class ColorRole {
public String id;
public String name;
public ColorRole(String id, String name) {
this.id = id;
this.name = name;
}
public static EmbedBuilder rolesListEmbed() {
List<ColorRole> roles = RoleColourUtility.getAllRoles();
EmbedBuilder embed = new EmbedBuilder()
.setTitle("Available ColorRoles")
.setColor(Color.GREEN)
.setFooter("Showing " + roles.size() + " roles");
StringBuilder builder = new StringBuilder();
roles.forEach(role -> {
builder.append(role.name).append("\n");
});
embed.addField("Roles:", builder.toString());
return embed;
}
}

View File

@ -1,70 +0,0 @@
/*
* Copyright (c) 2020. Aleksei Gryczewski
* All rights reserved.
*/
package dev.salmonllama.fsbot.utilities;
import org.javacord.api.DiscordApi;
import org.javacord.api.entity.message.embed.EmbedBuilder;
import java.awt.*;
public class Outfit {
private String id;
private String tag;
private String submitter;
private String link;
private DiscordApi api;
public Outfit(String id, String tag, String submitter, String link) {
this.id = id;
this.tag = tag;
this.submitter = submitter;
this.link = link;
}
public void setId(String id) { this.id = id; }
public String getId() { return this.id; }
public String getTag() { return this.tag; }
// public void setTag(String tag) { this.tag = tag; }
// public String getTag() { return this.tag; }
// public void setSubmitter(String submitter) { this.submitter = submitter; }
// public String getSubmitter() { return this.submitter; }
// public void setLink(String link) { this.link = link; }
// public String getLink() { return this.link; }
public EmbedBuilder generateEmbed() {
return new EmbedBuilder()
.setColor(Color.GREEN)
.setTitle(submitter + " | " + tag)
.setImage(this.link)
.setFooter(this.id);
}
public EmbedBuilder generateInfo() {
return new EmbedBuilder()
.setColor(Color.GREEN)
.setTitle("Outfit Information")
.setThumbnail(this.link)
.addField("Submitter:", this.submitter, true)
.addField("Tag:", this.tag, true)
.addField("Id:", this.id)
.addField("Link:", this.link);
}
public EmbedBuilder tagChangeEmbed(String changer, String oldTag, String newTag) {
return new EmbedBuilder()
.setColor(Color.YELLOW)
.setTitle(String.format("Tag changed by %s", changer))
.setThumbnail(this.link)
.addField("Old Tag:", oldTag)
.addField("New Tag:", newTag)
.setFooter(this.id);
}
}

View File

@ -1,246 +0,0 @@
/*
* Copyright (c) 2020. Aleksei Gryczewski
* All rights reserved.
*/
package dev.salmonllama.fsbot.utilities.database;
import com.rethinkdb.RethinkDB;
import com.rethinkdb.gen.ast.Table;
import com.rethinkdb.net.Connection;
import com.rethinkdb.net.Cursor;
import org.javacord.api.DiscordApi;
import dev.salmonllama.fsbot.utilities.Outfit;
import org.javacord.api.entity.server.Server;
import dev.salmonllama.fsbot.utilities.exceptions.OutfitNotFoundException;
import java.util.*;
public class DatabaseUtilities {
private final RethinkDB r;
private final Connection conn;
private final DiscordApi api;
public DatabaseUtilities(RethinkDB r, Connection conn, DiscordApi api) {
this.r = r;
this.conn = conn;
this.api = api;
}
private Table getTable(String table) {
if (this.r.db("fsbot").tableList().contains(table).run(this.conn) != null) {
return this.r.db("fsbot").table(table);
}
else {
return null;
}
}
private boolean validateId(String id) {
return r.db("fsbot").table("outfits").getField("id").contains(id).run(this.conn);
}
public boolean tagExists(String tag) {
return r.db("fsbot").table("outfits").getField("tag").distinct().contains(tag).run(this.conn);
}
public int countTags() {
return this.r.db("fsbot").table("outfits").getField("tag").distinct().count().run(this.conn);
}
public ArrayList<String> getTags() {
return r.db("fsbot").table("outfits").getField("tag").distinct().run(this.conn);
}
public Outfit getOutfitFromId(String id) throws OutfitNotFoundException {
if (!this.validateId(id)) throw new OutfitNotFoundException();
HashMap sample = this.r.db("fsbot").table("outfits")
.get(id)
.run(this.conn);
String tag = sample.get("tag").toString();
String link = sample.get("link").toString();
String submitterId = sample.get("submitter").toString();
String submitterName = api.getUserById(submitterId).join().getDiscriminatedName();
return new Outfit(id, tag, submitterName, link);
}
public String removeFromDatabase(String id) throws OutfitNotFoundException {
if (!this.validateId(id)) throw new OutfitNotFoundException();
// Remove outfit return deletion status. 0 = failed, >= 1 = success
HashMap deletion = r.db("fsbot").table("outfits").get(id).delete().run(conn);
return deletion.get("deleted").toString();
}
public Outfit randomOutfit() {
HashMap sample = this.r.db("fsbot").table("outfits")
.sample(1).nth(0)
.run(this.conn);
String id = sample.get("id").toString();
String tag = sample.get("tag").toString();
String link = sample.get("link").toString();
String submitterId = sample.get("submitter").toString();
String submitterName = api.getUserById(submitterId).join().getDiscriminatedName();
return new Outfit(id, tag, submitterName, link);
}
public Outfit randomTaggedOutfit(String targetTag) {
HashMap sample = r.db("fsbot").table("outfits")
.filter(row -> row.getField("tag").eq(targetTag))
.sample(1).nth(0)
.run(conn);
String id = String.valueOf(sample.get("id"));
String tag = String.valueOf(sample.get("tag"));
String link = String.valueOf(sample.get("link"));
String submitterId = String.valueOf(sample.get("submitter"));
String submitterName = api.getUserById(submitterId).join().getDiscriminatedName(); // Try a thenAccept with a thenApply
return new Outfit(id, tag, submitterName, link);
}
public String changeOutfitTag(String outfitId, String newTag) throws OutfitNotFoundException {
if (!this.validateId(outfitId)) throw new OutfitNotFoundException();
HashMap replacement = this.r.db("fsbot").table("outfits").get(outfitId).update(r.hashMap("tag", newTag)).run(this.conn);
return replacement.get("replaced").toString();
}
public Outfit getOutfitBySubmitter(String userId) {
HashMap sample = r.db("fsbot").table("outfits")
.filter(row -> row.getField("submitter").eq(userId))
.sample(1).nth(0)
.run(conn);
String id = String.valueOf(sample.get("id"));
String tag = String.valueOf(sample.get("tag"));
String link = String.valueOf(sample.get("link"));
String submitterId = String.valueOf(sample.get("submitter"));
String submitterName = api.getUserById(submitterId).join().getDiscriminatedName();
return new Outfit(id, tag, submitterName, link);
}
public long getSubmitterCount(String submitter) {
return r.db("fsbot").table("outfits")
.filter(
row -> row.getField("submitter").eq(submitter)
)
.count()
.run(conn);
}
public List<String> getSubmitterIds(String submitter) {
List<String> ids = new ArrayList<>();
Cursor<String> cursor = r.db("fsbot").table("outfits")
.filter(
row -> row.getField("submitter").eq(submitter)
)
.getField("id")
.run(conn);
cursor.forEach(ids::add);
return ids;
}
public void updateSubmitter(String submitter, String newSubmitter) {
// Add feature to return update-error?
r.db("fsbot").table("outfits")
.filter(r.hashMap("submitter", submitter))
.update(r.hashMap("submitter", newSubmitter))
.run(conn);
}
// public void newServerProcess(Server server) {
// if (this.r.db("fsbot").table("serverConf").contains(server.getIdAsString()).run(this.conn)) {
// return;
// }
// String serverName = server.getName();
// String serverId = server.getIdAsString();
// String logChannel = "null";
// String giveawayChannel = "null";
// String welcomeChannel = "null";
// String defaultWelcome = "welcome to the server";
// this.r.db("fsbot").table("serverConf").insert(
// this.r.hashMap("id", serverId)
// .with("name", serverName)
// .with("logChannel", logChannel)
// .with("giveawayChannel", giveawayChannel)
// .with("welcomeMsg", defaultWelcome)
// .with("welcomeChannel", welcomeChannel)
// .with("prefix", "~")
// ).run(this.conn);
// }
// public void tableSetup() { // TODO: Fix this -- invert conditionals, just create the tables. -> if *not* exist then create
// // Check for database existence, if not, create
// if (r.dbList().contains("fsbot").run(conn)) {
// // System.out.println("database 'fsbot' already exists.");
// }
// else {
// r.dbCreate("fsbot").run(conn);
// System.out.println("database fsbot did not exist, and has been created");
// }
// // Check for channels table existence, if not, create
// if (r.db("fsbot").tableList().contains("channels").run(conn)) {
// // System.out.println("table channels already exists");
// }
// else {
// r.db("fsbot").tableCreate("channels").run(conn);
// System.out.println("table channels did not exist, and has been created.");
// }
// // Check for serverconf table existence, if not, create
// if (r.db("fsbot").tableList().contains("serverConf").run(conn)) {
// // System.out.println("table serverConf already exists");
// }
// else {
// r.db("fsbot").tableCreate("serverConf").run(conn);
// System.out.println("table serverConf did not exist, and has been created");
// }
// // Check for permissions table existene, if not, create
// if (r.db("fsbot").tableList().contains("permissions").run(conn)) {
// // System.out.println("table permissions already exists");
// }
// else {
// r.db("fsbot").tableCreate("permissions").run(conn);
// System.out.println("table permissions did not exist and has been created");
// }
// // Check for outfits table existence, if not, create
// if (r.db("fsbot").tableList().contains("outfits").run(conn)) {
// // System.out.println("table outfits already exists");
// }
// else {
// r.db("fsbot").tableCreate("outfits").run(conn);
// System.out.println("table outfits did not exist and has been created");
// }
// // Check for colourRoles table existence, if not, create
// if (r.db("fsbot").tableList().contains("colourRoles").run(conn)) {
// // System.out.println("table colourRoles already exists");
// }
// else {
// r.db("fsbot").tableCreate("colourRoles").run(conn);
// System.out.println("table colourRoles did not exist and has been created");
// }
// }
}

View File

@ -1,68 +0,0 @@
/*
* Copyright (c) 2020. Aleksei Gryczewski
* All rights reserved.
*/
package dev.salmonllama.fsbot.utilities.database;
import com.rethinkdb.RethinkDB;
import com.rethinkdb.net.Connection;
import com.rethinkdb.net.Cursor;
import dev.salmonllama.fsbot.utilities.ColorRole;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class RoleColourUtility {
private static RethinkDB r = RethinkDB.r;
private static Connection conn = r.connection().hostname("localhost").port(28015).connect();
public static void addColourRole(String colourName, String roleId) {
r.db("fsbot").table("colourRoles").insert(
r.hashMap("id", roleId)
.with("name", colourName)).run(conn);
}
public static void deleteColourRole() {
}
public static String getColour(String colourName) {
String roleId = null;
Cursor cursor = r.db("fsbot").table("colourRoles")
.filter(row -> row.getField("name").eq(colourName))
.getField("id")
.run(conn);
List roleIds = cursor.toList();
for (Object id : roleIds) {
roleId = id.toString();
}
return roleId;
}
public static List<ColorRole> getAllRoles() {
List<ColorRole> allRoles = new ArrayList<>();
Cursor cursor = r.db("fsbot").table("colourRoles")
.run(conn);
while (cursor.hasNext()) {
HashMap role = (HashMap) cursor.next();
String id = String.valueOf(role.get("id"));
String name = String.valueOf(role.get("name"));
allRoles.add(new ColorRole(id, name));
}
return allRoles;
}
public static String getAllRoleInfo() {
return r.db("fsbot").table("colourRoles").run(conn);
}
}

View File

@ -1,74 +0,0 @@
/*
* Copyright (c) 2020. Aleksei Gryczewski
* All rights reserved.
*/
package dev.salmonllama.fsbot.utilities.database;
import com.rethinkdb.RethinkDB;
import com.rethinkdb.net.Connection;
import com.rethinkdb.net.Cursor;
import java.util.List;
public class ServerConfUtility {
private static final RethinkDB r = RethinkDB.r;
private static final Connection conn = r.connection().hostname("localhost").port(28015).connect();
private static String serverId;
public ServerConfUtility(String sId) {
serverId = sId;
}
// TODO: Turn server into method args, not class field.
public String getWelcomeMsg() {
String welcomeMsg = null;
Cursor welcomes = r.db("fsbot").table("serverConf")
.filter(row -> row.getField("id").eq(serverId))
.getField("welcomeMsg")
.run(conn);
List welcomeMsgs = welcomes.toList();
for (Object msg : welcomeMsgs) {
welcomeMsg = msg.toString();
}
return welcomeMsg;
}
public void setWelcomeMsg(String msg) {
r.db("fsbot").table("serverConf")
.get(serverId)
.update(r.hashMap("welcomeMsg", msg))
.run(conn);
}
public String getWelcomeChannel() {
String welcomeChannel = null;
Cursor channels = r.db("fsbot").table("serverConf")
.filter(row -> row.getField("id").eq(serverId))
.getField("welcomeChannel")
.run(conn);
List welcomeChannels = channels.toList();
for (Object chnl : welcomeChannels) {
welcomeChannel = chnl.toString();
}
return welcomeChannel;
}
public void setWelcomeChannel(String id) {
r.db("fsbot").table("serverConf")
.get(serverId)
.update(r.hashMap("welcomeChannel", id))
.run(conn);
}
}