Skip to content

Commit 1fe31c0

Browse files
authored
Implement mysql/mariadb locally to get rid of web database (#297)
* Now I know this sounds bad * Add first mysql query * Add sql dialects * I can mostly copy-paste this shit :D * Implement loading of guild settings * Bot boots on mysql * Edit keys in prod * Mutes and stuff * Most moderator actions * Finish mysql implementation * Strip out web database * Lint code * When you tell java to use UTC but it is still in local time * getting closer * It works * Cleanup * Remove sout * Undo one change * Cleanup * Cleanup
1 parent 240fc8b commit 1fe31c0

File tree

24 files changed

+1432
-1190
lines changed

24 files changed

+1432
-1190
lines changed

.idea/sqldialects.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bot/src/main/java/ml/duncte123/skybot/CommandManager.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@
7676
import javax.annotation.Nonnull;
7777
import javax.annotation.Nullable;
7878
import java.time.Instant;
79-
import java.time.OffsetDateTime;
8079
import java.time.ZoneOffset;
80+
import java.time.ZonedDateTime;
8181
import java.time.temporal.ChronoUnit;
8282
import java.util.*;
8383
import java.util.concurrent.*;
@@ -283,6 +283,7 @@ public CommandManager(Variables variables) {
283283
this.addCommand(new TagCommand(variables));
284284
this.addCommand(new TempBanCommand());
285285
this.addCommand(new TempMuteCommand());
286+
// this.addCommand(new TestFilterCommand());
286287
this.addCommand(new TestTagCommand());
287288
this.addCommand(new TheSearchCommand());
288289
this.addCommand(new ToggleAnnounceTracksCommand());
@@ -413,7 +414,7 @@ public void runCommand(MessageReceivedEvent event, String customPrefix) {
413414
}
414415

415416
public void setCooldown(String key, int seconds) {
416-
COOLDOWNS.put(key, OffsetDateTime.now(ZoneOffset.UTC).plusSeconds(seconds).toEpochSecond());
417+
COOLDOWNS.put(key, ZonedDateTime.now(ZoneOffset.UTC).plusSeconds(seconds).toEpochSecond());
417418
}
418419

419420
public long getRemainingCooldown(String key) {
@@ -703,9 +704,9 @@ private void addCommand(ICommand<CommandContext> command) {
703704
}
704705

705706
private static long calcTimeRemaining(long startTime) {
706-
// Get the start time as an OffsetDateTime
707-
final OffsetDateTime startTimeOffset = Instant.ofEpochSecond(startTime).atOffset(ZoneOffset.UTC);
707+
// Get the start time as an ZonedDateTime
708+
final ZonedDateTime startTimeOffset = Instant.ofEpochSecond(startTime).atZone(ZoneOffset.UTC);
708709
// get the time that is left for the cooldown
709-
return OffsetDateTime.now(ZoneOffset.UTC).until(startTimeOffset, ChronoUnit.SECONDS);
710+
return ZonedDateTime.now(ZoneOffset.UTC).until(startTimeOffset, ChronoUnit.SECONDS);
710711
}
711712
}

bot/src/main/java/ml/duncte123/skybot/SkyBot.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ private SkyBot() throws LoginException {
7878

7979
if ("psql".equals(useDatabase)) {
8080
logger.info("Using PostgreSQL as database impl");
81-
} else if ("web".equals(useDatabase)) {
82-
logger.warn("Using web api for all connections, please migrate to PostgreSQL");
81+
} else if ("mysql".equals(useDatabase)) {
82+
logger.warn("Using native MariaDB connection, please migrate to PostgreSQL");
8383
} else {
8484
shardManager = null; // for compiling n' stuff
8585
logger.error("Unknown database driver \"{}\", exiting!", useDatabase);

bot/src/main/java/ml/duncte123/skybot/Variables.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
import me.duncte123.weebJava.models.WeebApi;
3333
import me.duncte123.weebJava.types.TokenType;
3434
import ml.duncte123.skybot.database.AbstractDatabase;
35+
import ml.duncte123.skybot.database.MariaDBDatabase;
3536
import ml.duncte123.skybot.database.PostgreDatabase;
36-
import ml.duncte123.skybot.database.WebDatabase;
3737
import ml.duncte123.skybot.objects.DBMap;
3838
import ml.duncte123.skybot.objects.api.DuncteApis;
3939
import ml.duncte123.skybot.objects.apis.BlargBot;
@@ -172,13 +172,11 @@ public AbstractDatabase getDatabase() {
172172
return null;
173173
};
174174

175-
if ("psql".equals(this.config.useDatabase)) {
176-
this.database = new PostgreDatabase(this.config.jdbcURI, ohShitFn);
177-
} else if ("web".equals(this.config.useDatabase)) {
178-
this.database = new WebDatabase(this.getApis(), this.getJackson(), ohShitFn);
179-
} else {
180-
throw new IllegalArgumentException("Unknown database engine: " + this.config.useDatabase);
181-
}
175+
this.database = switch (this.config.useDatabase) {
176+
case "psql" -> new PostgreDatabase(this.config.jdbcURI, ohShitFn);
177+
case "mysql" -> new MariaDBDatabase(this.config.jdbcURI, ohShitFn);
178+
default -> throw new IllegalArgumentException("Unknown database engine: " + this.config.useDatabase);
179+
};
182180
}
183181

184182
return this.database;

bot/src/main/java/ml/duncte123/skybot/commands/essentials/RemindmeCommand.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import ml.duncte123.skybot.objects.command.Flag;
2727

2828
import javax.annotation.Nonnull;
29-
import java.time.OffsetDateTime;
29+
import java.time.ZonedDateTime;
3030
import java.util.List;
3131
import java.util.Map;
3232
import java.util.Optional;
@@ -111,7 +111,7 @@ public void execute(@Nonnull CommandContext ctx) {
111111
return;
112112
}
113113

114-
final OffsetDateTime expireDate = getDatabaseDate(duration);
114+
final ZonedDateTime expireDate = getDatabaseDate(duration);
115115

116116
createReminder(ctx, expireDate, reminder, flags, duration);
117117
}
@@ -125,7 +125,7 @@ private Optional<ParsedDuration> getDuration(Map<String, List<String>> flags) {
125125
}
126126
}
127127

128-
private void createReminder(CommandContext ctx, OffsetDateTime expireDate, String reminder, Map<String, List<String>> flags, ParsedDuration duration) {
128+
private void createReminder(CommandContext ctx, ZonedDateTime expireDate, String reminder, Map<String, List<String>> flags, ParsedDuration duration) {
129129
final boolean inChannel = flags.containsKey("c");
130130
final String where = inChannel ? " here" : "";
131131

bot/src/main/java/ml/duncte123/skybot/commands/guild/owner/CustomCommandCommand.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,19 @@ private void deleteOrShowCustomCommand(List<String> args, CommandContext ctx, Co
9595

9696
//Check for deleting
9797
if ("raw".equalsIgnoreCase(args.get(0))) {
98-
if (!commandExists(commandName, guildId, manager)) {
98+
final var cmd = manager.getCustomCommand(name, guildId);
99+
100+
if (cmd == null) {
99101
sendMsg(ctx, "No command was found for this name");
100102
return;
101103
}
102104

103-
final CustomCommand cmd = manager.getCustomCommand(commandName, guildId);
104105
final String escaped = cmd.getMessage().replaceAll("`", "");
105106
sendMsg(ctx, "Raw data for `" + commandName + "`:```pascal\n" + escaped + "\n```");
106107
} else if ("delete".equalsIgnoreCase(args.get(0)) || "remove".equalsIgnoreCase(args.get(0))) {
108+
final var cmd = manager.getCustomCommand(name, guildId);
107109

108-
if (!commandExists(commandName, guildId, manager)) {
110+
if (cmd == null) {
109111
sendMsg(ctx, "No command was found for this name");
110112
return;
111113
}
@@ -138,9 +140,10 @@ private void addOrEditCustomCommand(List<String> args, CommandContext ctx, Comma
138140

139141
final String commandAction = String.join(" ", args.subList(2, args.size()));
140142
final long guildId = ctx.getGuild().getIdLong();
143+
final var cmd = manager.getCustomCommand(name, guildId);
141144

142-
if (commandExists(commandName, guildId, manager)) {
143-
editCustomCommand(args, ctx, manager, commandName, commandAction, guildId);
145+
if (cmd != null) {
146+
editCustomCommand(args, ctx, manager, cmd, commandAction);
144147

145148
return;
146149
}
@@ -150,7 +153,7 @@ private void addOrEditCustomCommand(List<String> args, CommandContext ctx, Comma
150153

151154
private void createCustomCommand(CommandContext ctx, CommandManager manager, String commandName, String commandAction, long guildId) {
152155
try {
153-
final CommandResult result = registerCustomCommand(commandName, commandAction, guildId, manager);
156+
final CommandResult result = manager.registerCustomCommand(new CustomCommand(commandName, commandAction, guildId, false));
154157

155158
if (result == CommandResult.SUCCESS) {
156159
sendMsg(ctx, "Command added.");
@@ -176,22 +179,18 @@ private void createCustomCommand(CommandContext ctx, CommandManager manager, Str
176179
}
177180
}
178181

179-
private void editCustomCommand(List<String> args, CommandContext ctx, CommandManager manager, String commandName, String commandAction, long guildId) {
182+
private void editCustomCommand(List<String> args, CommandContext ctx, CommandManager manager, CustomCommand cmd, String newAction) {
180183
if (!"edit".equalsIgnoreCase(args.get(0)) && !"change".equalsIgnoreCase(args.get(0))) {
181-
sendMsg(ctx, "A command already exists for this server.");
184+
sendMsg(ctx, "A command with this name already exists for this server.");
182185

183186
return;
184187
}
185188

186-
if (editCustomCommand(manager.getCustomCommand(commandName, guildId), commandAction, manager)) {
189+
if (editCustomCommand(cmd, newAction, manager)) {
187190
sendMsg(ctx, "The command has been updated.");
188191
}
189192
}
190193

191-
private CommandResult registerCustomCommand(String name, String action, long guildId, CommandManager manager) {
192-
return manager.registerCustomCommand(new CustomCommand(name, action, guildId, false));
193-
}
194-
195194
private boolean editCustomCommand(@Nullable CustomCommand customCommand, String newMessage, CommandManager manager) {
196195
if (customCommand == null) {
197196
return false;
@@ -206,8 +205,4 @@ private boolean editCustomCommand(@Nullable CustomCommand customCommand, String
206205

207206
return manager.editCustomCommand(cmd);
208207
}
209-
210-
private boolean commandExists(String name, long guild, CommandManager manager) {
211-
return manager.getCustomCommand(name, guild) != null;
212-
}
213208
}

bot/src/main/java/ml/duncte123/skybot/listeners/ReadyShutdownListener.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,12 @@ private void onReady(ReadyEvent event) {
9191
TimeUnit.DAYS
9292
);
9393

94-
if ("psql".equals(this.variables.getConfig().useDatabase)) {
94+
if (
95+
"psql".equals(this.variables.getConfig().useDatabase) ||
96+
"mysql".equals(this.variables.getConfig().useDatabase)
97+
) {
9598
DataTimers.startReminderTimer(this.variables, LOGGER);
99+
DataTimers.startWarningTimer(this.variables, LOGGER);
96100
DataTimers.startUnbanTimer(this.variables, LOGGER);
97101
}
98102

@@ -138,6 +142,13 @@ private void onShutdown() {
138142
// shut down weeb.java
139143
this.variables.getWeebApi().shutdown();
140144
LOGGER.info("Weeb.java shutdown");
145+
146+
try {
147+
this.variables.getDatabase().close();
148+
} catch (Exception e) {
149+
LOGGER.error("Failed to close database", e);
150+
}
151+
141152
LOGGER.info("Bot and JDA shutdown cleanly");
142153
}
143154
}

bot/src/main/java/ml/duncte123/skybot/utils/AirUtils.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454

5555
import javax.annotation.Nonnull;
5656
import javax.annotation.Nullable;
57-
import java.time.OffsetDateTime;
5857
import java.time.ZoneOffset;
58+
import java.time.ZonedDateTime;
5959
import java.time.temporal.ChronoUnit;
6060
import java.util.ArrayList;
6161
import java.util.Arrays;
@@ -286,6 +286,8 @@ public static void handleExpiredReminders(List<Reminder> reminders, AbstractData
286286
errorResponse == ErrorResponse.CANNOT_SEND_TO_USER
287287
) {
288288
toPurge.add(reminder.getId());
289+
} else {
290+
errorResponseEx.printStackTrace();
289291
}
290292
}
291293
catch (Exception e) {
@@ -294,7 +296,7 @@ public static void handleExpiredReminders(List<Reminder> reminders, AbstractData
294296
}
295297

296298
// get a date that is 2 days in the future
297-
final OffsetDateTime plusTwoDays = OffsetDateTime.now(ZoneOffset.UTC).plus(2L, ChronoUnit.DAYS);
299+
final ZonedDateTime plusTwoDays = ZonedDateTime.now(ZoneOffset.UTC).plus(2L, ChronoUnit.DAYS);
298300

299301
// Remove any reminders that have not been removed after 2 days
300302
final List<Integer> extraRemoval = reminders.stream()

bot/src/main/java/ml/duncte123/skybot/utils/GuildSettingsUtils.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,4 @@ private static GuildSetting registerNewGuild(long guildId, Variables variables,
100100

101101
return newGuildSettings;
102102
}
103-
104-
public static void updateEmbedColor(long guildId, int color, Variables variables) {
105-
getGuild(guildId, variables).setEmbedColor(color);
106-
// TODO: save guild setting instead, we've deprecated this
107-
variables.getDatabase().updateOrCreateEmbedColor(guildId, color);
108-
}
109103
}

bot/src/main/java/ml/duncte123/skybot/utils/ModerationUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public static void handleUnmute(List<Mute> mutes, AbstractDatabase database, Var
197197

198198
final User targetUser = target.getUser();
199199

200-
LOG.debug("Unmuting " + mute.getUserTag());
200+
LOG.debug("Unmuting " + mute.getId());
201201

202202
final DunctebotGuild dbGuild = new DunctebotGuild(guild, variables);
203203
final long muteRoleId = dbGuild.getSettings().getMuteRoleId();

0 commit comments

Comments
 (0)