Adding RethinkDB
This commit is contained in:
parent
82ffe2bfcf
commit
529b2e7cd0
3 changed files with 97 additions and 2 deletions
|
|
@ -1,8 +1,8 @@
|
|||
package com.bbn.hadder;
|
||||
|
||||
import com.bbn.hadder.commands.Command;
|
||||
import com.bbn.hadder.commands.TestCommand;
|
||||
import com.bbn.hadder.commands.moderation.BanCommand;
|
||||
import com.bbn.hadder.commands.settings.PrefixCommand;
|
||||
import com.bbn.hadder.core.CommandHandler;
|
||||
import com.bbn.hadder.listener.*;
|
||||
import net.dv8tion.jda.api.entities.Activity;
|
||||
|
|
@ -33,6 +33,9 @@ public class Hadder {
|
|||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Rethink.connect();
|
||||
|
||||
|
||||
DefaultShardManagerBuilder builder = new DefaultShardManagerBuilder();
|
||||
|
||||
builder.setShardsTotal(1);
|
||||
|
|
@ -41,11 +44,14 @@ public class Hadder {
|
|||
|
||||
CommandHandler.cmdlist.put("test", new TestCommand());
|
||||
CommandHandler.cmdlist.put("ban", new BanCommand());
|
||||
CommandHandler.cmdlist.put("prefix", new PrefixCommand());
|
||||
|
||||
builder.addEventListeners(
|
||||
new MentionListener(),
|
||||
new PrivateMessageListener(),
|
||||
new CommandListener());
|
||||
new CommandListener(),
|
||||
new GuildJoinListener(),
|
||||
new GuildLeaveListener());
|
||||
|
||||
try {
|
||||
ShardManager shardManager = builder.build();
|
||||
|
|
|
|||
73
src/main/java/com/bbn/hadder/Rethink.java
Normal file
73
src/main/java/com/bbn/hadder/Rethink.java
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
package com.bbn.hadder;
|
||||
|
||||
import com.rethinkdb.RethinkDB;
|
||||
import com.rethinkdb.net.Connection;
|
||||
import com.rethinkdb.net.Cursor;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/*
|
||||
* @author Skidder / GregTCLTK
|
||||
*/
|
||||
|
||||
public class Rethink {
|
||||
private static RethinkDB r = RethinkDB.r;
|
||||
static Connection conn;
|
||||
|
||||
public static boolean connect() {
|
||||
try {
|
||||
conn = r.connection().hostname("127.0.0.1").db("Hadder").port(28015).connect();
|
||||
System.out.println("CONNECTED");
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.toString());
|
||||
System.out.println("CONNECTION FAILED");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void disconnect() {
|
||||
conn.close();
|
||||
System.out.println("DISCONNECTED");
|
||||
}
|
||||
|
||||
public static String get(String table, String where, String value, String column) {
|
||||
try {
|
||||
Cursor cursor = r.table(table).filter(row -> row.g(where.toLowerCase()).eq(value)).run(conn);
|
||||
if (cursor.hasNext()) {
|
||||
String sad = new JsonParser().parse(cursor.next().toString()).getAsJsonObject().get(column).toString();
|
||||
if (sad.startsWith("\"") && sad.endsWith("\"")) {
|
||||
return sad.substring(1, sad.length()-1);
|
||||
} else {
|
||||
return sad;
|
||||
}
|
||||
} else return null;
|
||||
} catch (NoSuchElementException e) {
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "<3";
|
||||
}
|
||||
|
||||
public static String update(String table, String whatvalue, String where, String wherevalue) {
|
||||
String out="";
|
||||
try {
|
||||
Cursor cursor = r.table(table).get(whatvalue).update(r.hashMap(where, wherevalue)).run(conn);
|
||||
out=cursor.toString();
|
||||
} catch (ClassCastException ignored) {}
|
||||
return out;
|
||||
}
|
||||
|
||||
public static String insertServer(String id) {
|
||||
String out = "";
|
||||
try {
|
||||
Cursor cursor = r.table("server")
|
||||
.insert(r.hashMap("id", id)
|
||||
.with("prefix", "h.")
|
||||
).run(conn);
|
||||
out = cursor.next().toString();
|
||||
} catch (ClassCastException ignored) {}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
16
src/main/java/com/bbn/hadder/listener/GuildJoinListener.java
Normal file
16
src/main/java/com/bbn/hadder/listener/GuildJoinListener.java
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
package com.bbn.hadder.listener;
|
||||
|
||||
/*
|
||||
* @author Skidder / GregTCLTK
|
||||
*/
|
||||
|
||||
import com.bbn.hadder.Rethink;
|
||||
import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class GuildJoinListener extends ListenerAdapter {
|
||||
public void onGuildJoin(@NotNull GuildJoinEvent event) {
|
||||
Rethink.insertServer(event.getGuild().getId());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue