Prevent error and remove db spamming

This commit is contained in:
Greg 2021-03-26 16:12:14 +01:00
parent 5cb7d83e00
commit 0553e9fe98
3 changed files with 6 additions and 20 deletions

View file

@ -20,10 +20,10 @@ import com.mongodb.BasicDBObject;
import com.mongodb.client.*; import com.mongodb.client.*;
import one.bbn.hadder.core.Config; import one.bbn.hadder.core.Config;
import org.bson.Document; import org.bson.Document;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.NoSuchElementException;
public class Mongo { public class Mongo {
@ -49,18 +49,13 @@ public class Mongo {
public JSONObject getObjectByID(String collection, String id) { public JSONObject getObjectByID(String collection, String id) {
BasicDBObject whereQuery = new BasicDBObject(); BasicDBObject whereQuery = new BasicDBObject();
whereQuery.put("id", id); whereQuery.put("id", id);
String response = db.getCollection(collection).find(whereQuery).cursor().next().toJson();
try { try {
String response = db.getCollection(collection).find(whereQuery).cursor().next().toJson();
return new JSONObject(response); return new JSONObject(response);
} catch (JSONException e) { } catch (NoSuchElementException e) {
insertUser(id); insertUser(id);
String response2 = db.getCollection(collection).find(whereQuery).cursor().next().toJson(); String response2 = db.getCollection(collection).find(whereQuery).cursor().next().toJson();
try { return new JSONObject(response2);
return new JSONObject(response2);
} catch (JSONException ex) {
ex.printStackTrace();
return null;
}
} }
} }

View file

@ -20,7 +20,6 @@ import one.bbn.hadder.core.Config;
import one.bbn.hadder.db.Mongo; import one.bbn.hadder.db.Mongo;
import one.bbn.hadder.utils.BotList; import one.bbn.hadder.utils.BotList;
import one.bbn.hadder.utils.MessageEditor; import one.bbn.hadder.utils.MessageEditor;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.events.guild.GuildJoinEvent; import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
import net.dv8tion.jda.api.events.guild.GuildLeaveEvent; import net.dv8tion.jda.api.events.guild.GuildLeaveEvent;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
@ -39,14 +38,6 @@ public class GuildListener extends ListenerAdapter {
} }
public void onGuildJoin(GuildJoinEvent e) { public void onGuildJoin(GuildJoinEvent e) {
new Thread(() -> {
for (Member member : e.getGuild().getMembers()) {
if (!member.getUser().getId().equals(e.getJDA().getSelfUser().getId())) {
mongo.insertUser(member.getUser().getId());
}
}
}).start();
mongo.insertGuild(e.getGuild().getId()); mongo.insertGuild(e.getGuild().getId());
e.getJDA().getTextChannelById("759783393230979142").sendMessage(new MessageEditor(null, null).getMessage(MessageEditor.MessageType.INFO) e.getJDA().getTextChannelById("759783393230979142").sendMessage(new MessageEditor(null, null).getMessage(MessageEditor.MessageType.INFO)
.setTitle("Joined Server") .setTitle("Joined Server")

View file

@ -19,6 +19,7 @@ package one.bbn.hadder.listener;
import one.bbn.hadder.core.Config; import one.bbn.hadder.core.Config;
import net.dv8tion.jda.api.events.ReadyEvent; import net.dv8tion.jda.api.events.ReadyEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.hooks.ListenerAdapter;
import one.bbn.hadder.utils.BotList;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -32,7 +33,6 @@ public class ReadyListener extends ListenerAdapter {
@Override @Override
public void onReady(@Nonnull ReadyEvent e) { public void onReady(@Nonnull ReadyEvent e) {
//TODO: Renew all bot lists new BotList(config).post();
//new BotList(config).post();
} }
} }