Refactored CreateGallery with new Database
This commit is contained in:
parent
3715209c9f
commit
6e6074b770
@ -8,6 +8,8 @@ package dev.salmonllama.fsbot.commands.developer;
|
|||||||
import com.rethinkdb.RethinkDB;
|
import com.rethinkdb.RethinkDB;
|
||||||
import com.rethinkdb.net.Connection;
|
import com.rethinkdb.net.Connection;
|
||||||
import dev.salmonllama.fsbot.config.BotConfig;
|
import dev.salmonllama.fsbot.config.BotConfig;
|
||||||
|
import dev.salmonllama.fsbot.database.controllers.GalleryController;
|
||||||
|
import dev.salmonllama.fsbot.database.models.GalleryChannel;
|
||||||
import dev.salmonllama.fsbot.guthix.Command;
|
import dev.salmonllama.fsbot.guthix.Command;
|
||||||
import dev.salmonllama.fsbot.guthix.CommandContext;
|
import dev.salmonllama.fsbot.guthix.CommandContext;
|
||||||
import dev.salmonllama.fsbot.guthix.CommandPermission;
|
import dev.salmonllama.fsbot.guthix.CommandPermission;
|
||||||
@ -17,6 +19,7 @@ import org.javacord.api.entity.message.embed.EmbedBuilder;
|
|||||||
import org.javacord.api.entity.server.Server;
|
import org.javacord.api.entity.server.Server;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
@ -34,63 +37,50 @@ public class CreateGalleryCommand extends Command { // TODO: This command needs
|
|||||||
static final Connection CONN = R.connection().hostname("localhost").port(28015).connect();
|
static final Connection CONN = R.connection().hostname("localhost").port(28015).connect();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCommand(CommandContext ctx) {
|
public void onCommand(CommandContext ctx) { // TODO: Might need some logic help...
|
||||||
Optional<Server> opServer = ctx.getServer();
|
if (ctx.isPrivateMessage()) {
|
||||||
TextChannel channel = ctx.getChannel();
|
ctx.reply("This command can only be used in a server!"); // TODO: Stop this. Turn this into a preset no-no embed.
|
||||||
String[] args = ctx.getArgs();
|
|
||||||
String targetChannelId = channel.getIdAsString();
|
|
||||||
String targetChannelName = channel.asServerChannel().get().getName(); // TODO: un-band-aid this.
|
|
||||||
|
|
||||||
if (!opServer.isPresent()) {
|
|
||||||
ctx.reply("This command can only be used in a server");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// Check if the channel is already a registered gallery channel.
|
||||||
|
// Create a gallery channel of the current channel.
|
||||||
|
// Store the gallery channel in the database.
|
||||||
|
|
||||||
Server server = opServer.get();
|
String channelId = ctx.getChannel().getIdAsString();
|
||||||
String targetServerName = server.getName();
|
try {
|
||||||
String targetServerId = server.getIdAsString();
|
if (GalleryController.galleryExists(channelId)) {
|
||||||
|
ctx.reply("A gallery already exists in this channel, can not create a new one.");
|
||||||
if (!server.getIdAsString().equals(BotConfig.HOME_SERVER)) {
|
return;
|
||||||
channel.sendMessage("Fashion galleries can only be created in the Fashionscape server");
|
}
|
||||||
return;
|
} catch (SQLException e) {
|
||||||
|
ctx.reply("An error occurred"); // TODO: error logging.
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args.length < 1) {
|
GalleryChannel gallery = new GalleryChannel();
|
||||||
channel.sendMessage("No Arguments provided");
|
gallery.channelId = channelId;
|
||||||
return;
|
String tag = ctx.getArgs()[0];
|
||||||
}
|
|
||||||
else if (args.length > 1) {
|
ctx.getServer().ifPresent(server -> {
|
||||||
channel.sendMessage("Too many arguments provided.");
|
gallery.serverId = server.getIdAsString();
|
||||||
return;
|
gallery.serverName = server.getName();
|
||||||
|
});
|
||||||
|
|
||||||
|
ctx.getChannel().asServerTextChannel().ifPresent(channel -> gallery.channelName = channel.getName());
|
||||||
|
|
||||||
|
try {
|
||||||
|
GalleryController.insert(gallery);
|
||||||
|
} catch(SQLException e) {
|
||||||
|
ctx.reply("An error occurred."); // TODO: error logging.
|
||||||
}
|
}
|
||||||
|
|
||||||
String tag = args[0];
|
EmbedBuilder embed = new EmbedBuilder()
|
||||||
|
.setColor(Color.GREEN)
|
||||||
if (R.db("fsbot").table("channels").getField("cId").contains(targetChannelId).run(CONN)) {
|
.addField("Success", "Channel added to storage with the following values:")
|
||||||
EmbedBuilder embed = new EmbedBuilder()
|
.addField("Channel Name:", gallery.channelName)
|
||||||
.setColor(Color.RED)
|
.addField("Channel Id:", gallery.channelId)
|
||||||
.addField("ERROR", "This channel is already gathering images");
|
.addField("Tag:", tag)
|
||||||
channel.sendMessage(embed);
|
.addField("End:", String.format("Table \"%s\" created for images in this channel", tag));
|
||||||
|
ctx.getChannel().sendMessage(embed);
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
R.db("fsbot").table("channels").insert(
|
|
||||||
R.hashMap("sName", targetServerName)
|
|
||||||
.with("sId", targetServerId)
|
|
||||||
.with("cName", targetChannelName)
|
|
||||||
.with("cId", targetChannelId)
|
|
||||||
.with("tag", tag)
|
|
||||||
).run(CONN);
|
|
||||||
|
|
||||||
R.db("fsbot").tableCreate(tag).run(CONN);
|
|
||||||
|
|
||||||
EmbedBuilder embed = new EmbedBuilder()
|
|
||||||
.setColor(Color.GREEN)
|
|
||||||
.addField("Success", "Channel added to storage with the following values:")
|
|
||||||
.addField("Channel Name:", targetChannelName)
|
|
||||||
.addField("Channel Id:", targetChannelId)
|
|
||||||
.addField("Tag:", tag)
|
|
||||||
.addField("End:", String.format("Table \"%s\" created for images in this channel", tag));
|
|
||||||
channel.sendMessage(embed);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,9 +33,19 @@ public class GalleryController {
|
|||||||
galleries.add(mapObject(rs));
|
galleries.add(mapObject(rs));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FSDB.get().close(rs);
|
||||||
return galleries;
|
return galleries;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean galleryExists(String channelId) throws SQLException {
|
||||||
|
ResultSet rs = FSDB.get().select("SELECT EXISTS(SELECT 1 AS exists FROM galleries WHERE channel_id = ?", channelId);
|
||||||
|
|
||||||
|
boolean exists = rs.getBoolean("exists");
|
||||||
|
|
||||||
|
FSDB.get().close(rs);
|
||||||
|
return exists;
|
||||||
|
}
|
||||||
|
|
||||||
private static GalleryChannel mapObject(ResultSet rs) throws SQLException {
|
private static GalleryChannel mapObject(ResultSet rs) throws SQLException {
|
||||||
GalleryChannel gallery = new GalleryChannel();
|
GalleryChannel gallery = new GalleryChannel();
|
||||||
gallery.serverId = rs.getString("server_id");
|
gallery.serverId = rs.getString("server_id");
|
||||||
|
Loading…
Reference in New Issue
Block a user