forked from TheCurle/Camelot
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move custom pings to their own module
- Loading branch information
1 parent
09ea9bd
commit 2ce941a
Showing
18 changed files
with
240 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
plugins { | ||
id 'camelot-module' | ||
} | ||
|
||
camelotModule { | ||
id = 'custom-pings' | ||
} | ||
|
||
dependencies { | ||
library(libs.bundles.database) | ||
library(libs.re2j) | ||
library(libs.fastutil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...ustom-pings/src/main/java/net/neoforged/camelot/module/custompings/CustomPingsModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package net.neoforged.camelot.module.custompings; | ||
|
||
import com.google.auto.service.AutoService; | ||
import com.jagrosh.jdautilities.command.CommandClientBuilder; | ||
import com.jagrosh.jdautilities.command.SlashCommand; | ||
import com.jagrosh.jdautilities.command.SlashCommandEvent; | ||
import net.dv8tion.jda.api.JDABuilder; | ||
import net.dv8tion.jda.api.Permission; | ||
import net.dv8tion.jda.api.entities.channel.ChannelType; | ||
import net.neoforged.camelot.Database; | ||
import net.neoforged.camelot.config.module.CustomPings; | ||
import net.neoforged.camelot.db.impl.PostCallbackDecorator; | ||
import net.neoforged.camelot.module.BuiltInModule; | ||
import net.neoforged.camelot.module.api.CamelotModule; | ||
import net.neoforged.camelot.module.custompings.db.PingsDAO; | ||
|
||
import java.util.Objects; | ||
|
||
@AutoService(CamelotModule.class) | ||
public class CustomPingsModule extends CamelotModule.WithDatabase<CustomPings> { | ||
public static volatile boolean isMigrating; | ||
|
||
public CustomPingsModule() { | ||
super(CustomPings.class); | ||
accept(BuiltInModule.DB_MIGRATION_CALLBACKS, builder -> builder | ||
.add(BuiltInModule.DatabaseSource.PINGS, 4, stmt -> { | ||
logger.info("Moving custom ping threads from pings.db to custom-pings.db"); | ||
var threads = stmt.executeQuery("select * from ping_threads"); | ||
db().useExtension(PingsDAO.class, db -> { | ||
while (threads.next()) { | ||
db.insertThread(threads.getLong(1), 0, threads.getLong(2)); | ||
} | ||
}); | ||
|
||
logger.info("Moving custom pings from pings.db to custom-pings.db"); | ||
isMigrating = true; | ||
var pings = stmt.executeQuery("select * from pings"); | ||
db().useExtension(PingsDAO.class, db -> { | ||
while (pings.next()) { | ||
db.insert(pings.getLong(2), pings.getLong(3), pings.getString(4), pings.getString(5)); | ||
} | ||
}); | ||
isMigrating = false; | ||
|
||
CustomPingListener.requestRefresh(); | ||
})); | ||
|
||
accept(BuiltInModule.CONFIGURATION_COMMANDS, builder -> builder | ||
.accept(new SlashCommand() { | ||
{ | ||
name = "custom-pings-threads-channel"; | ||
help = "Sets the channel for custom pings threads in this guild to this channel"; | ||
} | ||
@Override | ||
protected void execute(SlashCommandEvent event) { | ||
if (event.getChannel().getType() != ChannelType.TEXT) { | ||
event.reply("This command can only be used in text channels.").setEphemeral(true).queue(); | ||
return; | ||
} | ||
if (!event.getGuild().getSelfMember().hasPermission(event.getTextChannel(), Permission.CREATE_PRIVATE_THREADS)) { | ||
event.reply("I cannot create private threads in this channel!").setEphemeral(true).queue(); | ||
return; | ||
} | ||
|
||
db().useExtension(PingsDAO.class, db -> { | ||
var old = Objects.requireNonNullElse(db.getPingThreadsChannel(event.getGuild().getIdLong()), 0L); | ||
db.setPingThreadsChannel(event.getGuild().getIdLong(), event.getChannel().getIdLong()); | ||
event.reply("Set ping threads channel to this channel!" + (old != 0 ? " (was <#" + old + ">)" : "")).queue(); | ||
}); | ||
} | ||
})); | ||
} | ||
|
||
@Override | ||
public void init() { | ||
super.init(); | ||
CustomPingListener.requestRefresh(); | ||
} | ||
|
||
@Override | ||
public String id() { | ||
return "custom-pings"; | ||
} | ||
|
||
@Override | ||
public void registerCommands(CommandClientBuilder builder) { | ||
builder.addSlashCommand(new CustomPingsCommand()); | ||
} | ||
|
||
@Override | ||
public void registerListeners(JDABuilder builder) { | ||
builder.addEventListeners(new CustomPingListener()); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...et/neoforged/camelot/db/schemas/Ping.java → ...d/camelot/module/custompings/db/Ping.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 6 additions & 4 deletions
10
...d/camelot/db/callback/PingsCallbacks.java → ...module/custompings/db/PingsCallbacks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.