Skip to content

Commit 8795d00

Browse files
fixed ServerLock & StarBoard
1 parent 6b1c4f2 commit 8795d00

File tree

5 files changed

+119
-114
lines changed

5 files changed

+119
-114
lines changed

src/main/java/com/javadiscord/javabot/events/GuildJoin.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.javadiscord.javabot.events;
22

33
import com.javadiscord.javabot.Bot;
4+
import com.javadiscord.javabot.other.Database;
45
import com.mongodb.client.MongoCollection;
56
import com.mongodb.client.MongoDatabase;
67
import net.dv8tion.jda.api.entities.Guild;
@@ -18,10 +19,8 @@ public static void addGuildToDB (Guild guild) {
1819
MongoDatabase database = mongoClient.getDatabase("other");
1920
MongoCollection<Document> collection = database.getCollection("config");
2021

21-
// TODO: fix this using the new file based config
22-
2322
if (collection.find(eq("guild_id", guild.getId())).first() == null) {
24-
//new Database().insertGuildDoc(guild);
23+
new Database().insertGuildDoc(guild);
2524
}
2625
}
2726

src/main/java/com/javadiscord/javabot/events/StarboardListener.java

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.javadiscord.javabot.other.Database;
66
import com.mongodb.client.MongoCollection;
77
import com.mongodb.client.MongoCursor;
8+
import lombok.extern.slf4j.Slf4j;
89
import net.dv8tion.jda.api.EmbedBuilder;
910
import net.dv8tion.jda.api.entities.*;
1011
import net.dv8tion.jda.api.events.message.guild.GuildMessageDeleteEvent;
@@ -19,12 +20,10 @@
1920
import static com.javadiscord.javabot.events.Startup.mongoClient;
2021
import static com.mongodb.client.model.Filters.eq;
2122

23+
@Slf4j
2224
public class StarboardListener extends ListenerAdapter {
2325

24-
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(StarboardListener.class);
25-
2626
void addToSB(Guild guild, MessageChannel channel, Message message, int starCount) {
27-
2827
String gID = guild.getId();
2928
String cID = channel.getId();
3029
String mID = message.getId();
@@ -39,10 +38,8 @@ void addToSB(Guild guild, MessageChannel channel, Message message, int starCount
3938
.setColor(Constants.GRAY)
4039
.setDescription(message.getContentRaw());
4140

42-
// TODO: fix this using the new file based config
43-
4441
MessageAction msgAction = sc
45-
.sendMessage("db.getConfigString(guild, other.starboard.starboard_emote)" + " " +
42+
.sendMessage(Bot.config.get(guild).getStarBoard().getEmotes().get(0) + " " +
4643
starCount + " | " + message.getTextChannel().getAsMention())
4744
.setEmbeds(eb.build());
4845

@@ -64,9 +61,7 @@ void removeFromSB(Guild guild, String mID) {
6461

6562
Document doc = collection.find(eq("message_id", mID)).first();
6663
if (doc == null) return;
67-
6864
String var = doc.getString("starboard_embed");
69-
7065
Bot.config.get(guild).getStarBoard().getChannel()
7166
.retrieveMessageById(var)
7267
.complete()
@@ -77,13 +72,10 @@ void removeFromSB(Guild guild, String mID) {
7772
}
7873

7974
void updateSB(Guild guild, String cID, String mID, int reactionCount) {
80-
8175
Database db = new Database();
8276
String gID = guild.getId();
83-
8477
String sbcEmbedId = db.getStarboardChannelString(gID, cID, mID, "starboard_embed");
8578
if (sbcEmbedId == null) return;
86-
8779
Message sbMsg;
8880

8981
try {
@@ -93,24 +85,23 @@ void updateSB(Guild guild, String cID, String mID, int reactionCount) {
9385

9486
if (sbMsg == null) return;
9587

88+
var config = Bot.config.get(guild).getStarBoard();
9689
if (reactionCount > 0) {
97-
String starLevel = "starboard_emote";
90+
String starEmote = config.getEmotes().get(0);
9891
if (reactionCount > 10)
99-
starLevel = "starboard_emote2";
92+
starEmote = config.getEmotes().get(1);
10093
if (reactionCount > 25)
101-
starLevel = "starboard_emote3";
102-
103-
// TODO: fix this using the new file based config
94+
starEmote = config.getEmotes().get(2);
10495

10596
TextChannel tc = guild.getTextChannelById(cID);
106-
sbMsg.editMessage("db.getConfigString(guild, other.starboard. + starLevel)" + " "
97+
sbMsg.editMessage(starEmote + " "
10798
+ reactionCount + " | " + tc.getAsMention()).queue();
10899

109100
} else { removeFromSB(guild, mID); }
110101
}
111102

112103
public void updateAllSBM(Guild guild) {
113-
logger.info("[{}] Updating Starboard Messages", guild.getName());
104+
log.info("[{}] Updating Starboard Messages", guild.getName());
114105

115106
String gID = guild.getId();
116107
MongoCollection<Document> collection = mongoClient
@@ -125,15 +116,12 @@ public void updateAllSBM(Guild guild) {
125116

126117
String cID = doc.getString("channel_id");
127118
String mID = doc.getString("message_id");
128-
129119
Message msg;
130120

131121
try { msg = guild.getTextChannelById(cID).retrieveMessageById(mID).complete();
132122
} catch (ErrorResponseException e) { collection.deleteOne(doc); continue; }
133123

134-
// TODO: fix this using the new file based config
135-
136-
String emote = "";//db.getConfigString(guild, "other.starboard.starboard_emote");
124+
String emote = Bot.config.get(guild).getStarBoard().getEmotes().get(0);
137125
if (msg.getReactions().isEmpty()) updateSB(guild, cID, mID, 0);
138126

139127
int reactionCount = msg
@@ -159,10 +147,8 @@ else if (db.getStarboardChannelString(gID, cID, mID, "starboard_embed") != null)
159147
public void onGuildMessageReactionAdd(GuildMessageReactionAddEvent event) {
160148
if (event.getUser().isBot()) return;
161149

162-
// TODO: fix this using the new file based config
163-
164150
Database db = new Database();
165-
String sbEmote = "";//db.getConfigString(event.getGuild(), "other.starboard.starboard_emote");
151+
String sbEmote = Bot.config.get(event.getGuild()).getStarBoard().getEmotes().get(0);
166152

167153
if (!event.getReactionEmote().getName().equals(sbEmote)) return;
168154

@@ -188,10 +174,8 @@ public void onGuildMessageReactionAdd(GuildMessageReactionAddEvent event) {
188174
public void onGuildMessageReactionRemove(GuildMessageReactionRemoveEvent event) {
189175
if (event.getUser().isBot()) return;
190176

191-
// TODO: fix this using the new file based config
192-
193177
Database db = new Database();
194-
String sbEmote = ""; //db.getConfigString(event.getGuild(), "other.starboard.starboard_emote");
178+
String sbEmote = Bot.config.get(event.getGuild()).getStarBoard().getEmotes().get(0);
195179

196180
if (!event.getReactionEmote().getName().equals(sbEmote)) return;
197181

src/main/java/com/javadiscord/javabot/events/UserLeave.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.javadiscord.javabot.events;
22

33
import com.javadiscord.javabot.Bot;
4+
import com.javadiscord.javabot.other.Database;
45
import com.javadiscord.javabot.other.StatsCategory;
56
import net.dv8tion.jda.api.entities.Emote;
67
import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent;
@@ -14,30 +15,23 @@ public class UserLeave extends ListenerAdapter {
1415
@Override
1516
public void onGuildMemberRemove(GuildMemberRemoveEvent event) {
1617

17-
// TODO: fix this using the new file based config
18-
19-
if (true) {//!new Database().getConfigBoolean(event.getGuild(), "other.server_lock.lock_status")) {
18+
if (!new Database().getConfigBoolean(event.getGuild(), "other.server_lock.lock_status")) {
2019
var welcomeConfig = Bot.config.get(event.getGuild()).getWelcome();
2120
if (welcomeConfig.isEnabled()) {
22-
2321
String leaveMessage = Objects.requireNonNull(welcomeConfig.getLeaveMessageTemplate());
2422
String replacedText;
2523

2624
if (event.getUser().isBot()) {
2725
List<Emote> Emote = event.getGuild().getEmotesByName("badgeBot", false);
2826
replacedText = leaveMessage.replace("{!boticon}", " " + Emote.get(0).getAsMention());
29-
} else {
30-
replacedText = leaveMessage.replace("{!boticon}", "");
31-
}
27+
} else replacedText = leaveMessage.replace("{!boticon}", "");
3228

3329
String replacedText2 = replacedText
3430
.replace("{!member}", event.getUser().getAsMention())
3531
.replace("{!membertag}", event.getUser().getAsTag())
3632
.replace("{!server}", event.getGuild().getName());
37-
3833
welcomeConfig.getChannel().sendMessage(replacedText2).queue();
3934
}
40-
4135
StatsCategory.update(event.getGuild());
4236
}
4337
}

src/main/java/com/javadiscord/javabot/other/Database.java

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.mongodb.BasicDBObject;
44
import com.mongodb.MongoClient;
55
import com.mongodb.client.MongoCollection;
6+
import com.mongodb.client.MongoDatabase;
67
import net.dv8tion.jda.api.entities.Guild;
78
import net.dv8tion.jda.api.entities.Member;
89
import net.dv8tion.jda.api.entities.Message;
@@ -34,9 +35,9 @@ public class Database {
3435
*/
3536
private static final Map<String, String[]> DB_COLS = new HashMap<>();
3637
static {
37-
DB_COLS.put("userdata", new String[] { "potential_bot_list", "users", "warns" });
38-
DB_COLS.put("other", new String[] { "config", "customcommands", "expert_questions",
39-
"open_submissions", "reactionroles", "starboard_messages", "submission_messages" });
38+
DB_COLS.put("userdata", new String[] {"potential_bot_list", "users", "warns"});
39+
DB_COLS.put("other", new String[] {"config", "customcommands", "expert_questions",
40+
"open_submissions", "reactionroles", "starboard_messages", "submission_messages"});
4041
}
4142

4243
List<String> getDatabases(MongoClient mongoClient) {
@@ -61,6 +62,7 @@ public void databaseCheck(MongoClient mongoClient, List<Guild> guilds) {
6162
}
6263
}
6364
}
65+
for (var g : guilds) if (!guildDocExists(g)) insertGuildDoc(g);
6466
}
6567

6668
public void deleteOpenSubmissions(Guild guild) {
@@ -92,6 +94,34 @@ public Document userDoc(User user) {
9294
.append("qotwpoints", 0);
9395
}
9496

97+
public Document guildDoc (String guildID) {
98+
Document lock = new Document("lock_status", false)
99+
.append("lock_count", 0);
100+
101+
Document other = new Document()
102+
.append("server_lock", lock);
103+
104+
Document doc = new Document()
105+
.append("guild_id", guildID)
106+
.append("other", other);
107+
return doc;
108+
}
109+
110+
public boolean guildDocExists(Guild guild) {
111+
MongoDatabase database = mongoClient.getDatabase("other");
112+
MongoCollection<Document> collection = database.getCollection("config");
113+
if (guild == null) return false;
114+
if (collection.find(eq("guild_id", guild.getId())).first() == null) return false;
115+
return true;
116+
}
117+
118+
public void insertGuildDoc (Guild guild) {
119+
MongoDatabase database = mongoClient.getDatabase("other");
120+
MongoCollection<Document> collection = database.getCollection("config");
121+
collection.insertOne(guildDoc(guild.getId()));
122+
logger.warn("Added Database entry for Guild \"" + guild.getName() + "\" (" + guild.getId() + ")");
123+
}
124+
95125
public void setMemberEntry(String memberID, String path, Object newValue) {
96126
Document setData = new Document(path, newValue);
97127
Document update = new Document("$set", setData);
@@ -119,25 +149,59 @@ private <T> T getMember(Member member, String path, T defaultValue) {
119149
} catch (Exception e) { e.printStackTrace(); return defaultValue; }
120150
}
121151

122-
public String getMemberString(Member member, String path) {
123-
return getMember(member, path, "None");
124-
}
125-
126152
public int getMemberInt(Member member, String path) {
127153
return getMember(member, path, 0);
128154
}
129155

156+
private <T> T getConfig(Guild guild, String path, T defaultValue) {
157+
try {
158+
Document doc = mongoClient
159+
.getDatabase("other")
160+
.getCollection("config")
161+
.find(eq("guild_id", guild.getId()))
162+
.first();
163+
String[] splittedPath = path.split("\\.");
164+
int pathLen = splittedPath.length - 1;
165+
166+
for (int i = 0; i < pathLen; ++i) {
167+
doc = doc.get(splittedPath[i], Document.class);
168+
}
169+
return doc.get(splittedPath[pathLen], defaultValue);
170+
} catch (Exception e) {
171+
e.printStackTrace();
172+
if (!guildDocExists(guild)) {
173+
insertGuildDoc(guild);
174+
}
175+
return defaultValue;
176+
}
177+
}
178+
179+
public boolean getConfigBoolean(Guild guild, String path) {
180+
return getConfig(guild, path, false);
181+
}
182+
183+
public int getConfigInt(Guild guild, String path) {
184+
return getConfig(guild, path, 0);
185+
}
186+
187+
public void setConfigEntry(String guildID, String path, Object newValue) {
188+
BasicDBObject setData = new BasicDBObject(path, newValue);
189+
BasicDBObject update = new BasicDBObject("$set", setData);
190+
Document query = new Document("guild_id", guildID);
191+
mongoClient.getDatabase("other")
192+
.getCollection("config")
193+
.updateOne(query, update);
194+
}
195+
130196
public boolean isMessageOnStarboard(String gID, String cID, String mID) {
131197
BasicDBObject criteria = new BasicDBObject("guild_id", gID)
132198
.append("channel_id", cID)
133199
.append("message_id", mID);
134-
135200
Document first = mongoClient
136201
.getDatabase("other")
137202
.getCollection("starboard_messages")
138203
.find(criteria)
139204
.first();
140-
141205
return first != null;
142206
}
143207

0 commit comments

Comments
 (0)