Skip to content

Commit

Permalink
Update Parent & Spotless apply
Browse files Browse the repository at this point in the history
  • Loading branch information
dfuchss committed Sep 2, 2024
1 parent 70df5e6 commit 3735f38
Show file tree
Hide file tree
Showing 31 changed files with 216 additions and 100 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.fuchss</groupId>
<artifactId>maven-parent</artifactId>
<version>0.21.31</version>
<version>0.21.34</version>
</parent>

<artifactId>deltabot</artifactId>
Expand Down
8 changes: 2 additions & 6 deletions src/main/kotlin/org/fuchss/deltabot/BotConfiguration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ class BotConfiguration {

fun runInDocker() = System.getenv("RUN_IN_DOCKER") == "true"

fun isAdmin(user: User): Boolean {
return admins.any { u -> u.discordId == user.id }
}
fun isAdmin(user: User): Boolean = admins.any { u -> u.discordId == user.id }

fun toggleAdmin(user: User): Boolean {
if (admins.any { u -> u.discordId == user.id }) {
Expand All @@ -86,9 +84,7 @@ class BotConfiguration {
return admins.any { u -> u.discordId == user.id }
}

fun getAdmins(jda: JDA): List<User> {
return admins.mapNotNull { u -> jda.fetchUser(u.discordId) }
}
fun getAdmins(jda: JDA): List<User> = admins.mapNotNull { u -> jda.fetchUser(u.discordId) }

fun toggleDebug(): Boolean {
debug = !debug
Expand Down
8 changes: 6 additions & 2 deletions src/main/kotlin/org/fuchss/deltabot/Language.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import org.fuchss.objectcasket.objectpacker.port.Session
/**
* Definition of all supported languages with their locales.
*/
enum class Language(val locale: String) {
enum class Language(
val locale: String
) {
ENGLISH("en_GB"),
DEUTSCH("de_DE");

Expand All @@ -20,7 +22,9 @@ enum class Language(val locale: String) {
}
}

class LanguageSettings(private val session: Session) {
class LanguageSettings(
private val session: Session
) {
private var languages: MutableSet<LanguageDTO> = mutableSetOf()

init {
Expand Down
17 changes: 9 additions & 8 deletions src/main/kotlin/org/fuchss/deltabot/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ fun main() {

val builder = JDABuilder.createDefault(token).enableIntents(GatewayIntent.MESSAGE_CONTENT)
val jda =
builder.addEventListeners(
scheduler,
LoggerListener(config),
ActivityChanger(),
ReactionHandler(),
commandRegistry,
CommandHandler(config, database, commandRegistry)
).build()
builder
.addEventListeners(
scheduler,
LoggerListener(config),
ActivityChanger(),
ReactionHandler(),
commandRegistry,
CommandHandler(config, database, commandRegistry)
).build()

initHiddenMessages(jda, scheduler, database)
jda.awaitReady()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import java.util.StringJoiner
/**
* The implementation of an interface to a Duckling service at a certain [endpoint url][endpoint].
*/
class DucklingService(private val endpoint: String) {
class DucklingService(
private val endpoint: String
) {
fun interpretTime(
text: String,
language: Language
Expand Down Expand Up @@ -46,14 +48,13 @@ class DucklingService(private val endpoint: String) {
}
}

private fun extractTime(t: DucklingResponseValue): String {
return if (t.value == null) {
private fun extractTime(t: DucklingResponseValue): String =
if (t.value == null) {
// From & To are set ..
t.from!!.value!!
} else {
t.value!!
}
}

private fun getDataString(params: Map<String, Any>): String {
val result = StringJoiner("&")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import org.fuchss.objectcasket.objectpacker.port.Session
* @param[session] the session (DB) of the bot
* @param[commandRegistry] the registry of all commands
*/
class CommandHandler(private val configuration: BotConfiguration, private val session: Session, private val commandRegistry: ICommandRegistry) : EventListener {
class CommandHandler(
private val configuration: BotConfiguration,
private val session: Session,
private val commandRegistry: ICommandRegistry
) : EventListener {
private var nameToCommand: Map<String, BotCommand> = commandRegistry.nameToCommand()

init {
Expand Down
14 changes: 11 additions & 3 deletions src/main/kotlin/org/fuchss/deltabot/command/CommandRegistry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ import org.fuchss.objectcasket.objectpacker.port.Session
* @param[scheduler] the scheduler of the bot
* @param[session] the session / databse of the bot
*/
class CommandRegistry(private val configuration: BotConfiguration, dbLocation: String, scheduler: Scheduler, session: Session) :
ICommandRegistry,
class CommandRegistry(
private val configuration: BotConfiguration,
dbLocation: String,
scheduler: Scheduler,
session: Session
) : ICommandRegistry,
EventListener {
private val pollAdmin: IPollAdmin = PollAdmin()

Expand Down Expand Up @@ -101,7 +105,11 @@ class CommandRegistry(private val configuration: BotConfiguration, dbLocation: S
globalCommands.remove(command)
updateHooks.forEach { r -> r.run() }

jda.fetchCommands().find { c -> c.name == command.createCommand().name }?.delete()?.complete()
jda
.fetchCommands()
.find { c -> c.name == command.createCommand().name }
?.delete()
?.complete()
}

override fun permissions(command: Command): CommandPermissions {
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/org/fuchss/deltabot/command/admin/Admin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import org.fuchss.deltabot.command.GuildCommand
/**
* A [BotCommand] that toggles the admin state for a user.
*/
class Admin(private val configuration: BotConfiguration) : GuildCommand {
class Admin(
private val configuration: BotConfiguration
) : GuildCommand {
override val permissions: CommandPermissions get() = CommandPermissions.ADMIN

override fun createCommand(guild: Guild): SlashCommandData {
Expand Down
19 changes: 15 additions & 4 deletions src/main/kotlin/org/fuchss/deltabot/command/admin/Channels.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class Channels : GuildCommand {
SubcommandData("new", "add a new role with text and voice channel").addOptions(
OptionData(OptionType.STRING, "name", "the name of the role").setRequired(true),
OptionData(OptionType.BOOLEAN, "only-text", "only create a text channel (default: false)").setRequired(false),
OptionData(OptionType.STRING, "color", "the color to associate with the new role (default: none)").setRequired(false)
OptionData(OptionType.STRING, "color", "the color to associate with the new role (default: none)")
.setRequired(false)
.addChoices(COLORS.toSortedMap().map { c -> Command.Choice(c.key, c.value.toString()) })
),
SubcommandData("set", "add a new channel with a specific name for a certain role").addOptions(
Expand Down Expand Up @@ -86,7 +87,13 @@ class Channels : GuildCommand {
}

val hook = event.deferReply().complete()
val role = guild.createRole().setName(name).setMentionable(true).setColor(color).complete()
val role =
guild
.createRole()
.setName(name)
.setMentionable(true)
.setColor(color)
.complete()

createChannel(guild, role, name, onlyText)
hook.editOriginal("Welcome # on the server!".translate(event, role.asMention)).queue()
Expand All @@ -111,7 +118,9 @@ class Channels : GuildCommand {
val guildRoleOfBot = guild.getRoleByBot(guild.jda.selfUser)!!

return if (onlyText) {
guild.createTextChannel(name).addRolePermissionOverride(role.idLong, listOf(Permission.VIEW_CHANNEL), null) //
guild
.createTextChannel(name)
.addRolePermissionOverride(role.idLong, listOf(Permission.VIEW_CHANNEL), null) //
.addRolePermissionOverride(
guildRoleOfBot.idLong,
listOf(Permission.VIEW_CHANNEL, Permission.MANAGE_CHANNEL, Permission.MESSAGE_MANAGE),
Expand All @@ -120,7 +129,9 @@ class Channels : GuildCommand {
.addRolePermissionOverride(everyone.idLong, null, listOf(Permission.VIEW_CHANNEL)) //
.complete()
} else {
guild.createVoiceChannel(name).addRolePermissionOverride(role.idLong, listOf(Permission.VIEW_CHANNEL), null) //
guild
.createVoiceChannel(name)
.addRolePermissionOverride(role.idLong, listOf(Permission.VIEW_CHANNEL), null) //
.addRolePermissionOverride(
guildRoleOfBot.idLong,
listOf(Permission.VIEW_CHANNEL, Permission.MANAGE_CHANNEL, Permission.MESSAGE_MANAGE),
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/org/fuchss/deltabot/command/admin/Debug.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import org.fuchss.deltabot.command.GlobalCommand
/**
* A [BotCommand] that toggles the debug state.
*/
class Debug(private val configuration: BotConfiguration) : GlobalCommand {
class Debug(
private val configuration: BotConfiguration
) : GlobalCommand {
override val permissions: CommandPermissions get() = CommandPermissions.ADMIN

override fun createCommand(): SlashCommandData {
return Commands.slash("debug", "toggle the debug flag")
}
override fun createCommand(): SlashCommandData = Commands.slash("debug", "toggle the debug flag")

override fun handle(event: SlashCommandInteraction) {
event.reply("Debug is now ${configuration.toggleDebug()}").setEphemeral(true).queue()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import org.fuchss.objectcasket.objectpacker.port.Session
/**
* A [BotCommand] that toggles the admin state for a user in a guild.
*/
class GuildAdmin(private val session: Session) : GuildCommand {
class GuildAdmin(
private val session: Session
) : GuildCommand {
override val permissions: CommandPermissions get() = CommandPermissions.GUILD_ADMIN

override fun createCommand(guild: Guild): SlashCommandData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import java.util.function.BiConsumer
/**
* A [BotCommand] that performs the task of creation of an initial admin user.
*/
class InitialAdminCommand(private val configuration: BotConfiguration, private val adminAddedCallback: BiConsumer<JDA, User>) : GlobalCommand {
class InitialAdminCommand(
private val configuration: BotConfiguration,
private val adminAddedCallback: BiConsumer<JDA, User>
) : GlobalCommand {
override val permissions: CommandPermissions get() = CommandPermissions.ALL

override fun createCommand(): SlashCommandData = Commands.slash("initial-admin", "create an initial admin user")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import org.fuchss.objectcasket.objectpacker.port.Session
/**
* A [BotCommand] that prints a persistent help message.
*/
class PersistentHelp(configuration: BotConfiguration, session: Session, registry: ICommandRegistry) : Help(configuration, session, registry) {
class PersistentHelp(
configuration: BotConfiguration,
session: Session,
registry: ICommandRegistry
) : Help(configuration, session, registry) {
override val permissions: CommandPermissions get() = CommandPermissions.GUILD_ADMIN

override fun createCommand(): SlashCommandData {
return Commands.slash("help-persist", "Prints a help message that will be persisted")
}
override fun createCommand(): SlashCommandData = Commands.slash("help-persist", "Prints a help message that will be persisted")

override fun handle(event: SlashCommandInteraction) {
var commands = if (event.isFromGuild) event.guild!!.fetchCommands() + event.jda.fetchCommands() else event.jda.fetchCommands()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ import java.io.File
/**
* A [BotCommand] that resets the command states of all bot commands in discord.
*/
class ResetStateAndCommands(private val configuration: BotConfiguration, private val dbLocation: String, private val session: Session) : GlobalCommand {
class ResetStateAndCommands(
private val configuration: BotConfiguration,
private val dbLocation: String,
private val session: Session
) : GlobalCommand {
override val permissions: CommandPermissions get() = CommandPermissions.ADMIN

override fun createCommand(): SlashCommandData {
return Commands.slash("deinit", "main reset command. resets the states and deletes all registered commands")
}
override fun createCommand(): SlashCommandData = Commands.slash("deinit", "main reset command. resets the states and deletes all registered commands")

override fun handle(event: SlashCommandInteraction) {
if (!configuration.isAdmin(event.user)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import org.fuchss.deltabot.command.GlobalCommand
class Shutdown : GlobalCommand {
override val permissions: CommandPermissions get() = CommandPermissions.ADMIN

override fun createCommand(): SlashCommandData {
return Commands.slash("shutdown", "Shutdown/Restart the bot")
}
override fun createCommand(): SlashCommandData = Commands.slash("shutdown", "Shutdown/Restart the bot")

override fun handle(event: SlashCommandInteraction) {
event.reply("Shutting down").setEphemeral(true).complete()
Expand Down
20 changes: 15 additions & 5 deletions src/main/kotlin/org/fuchss/deltabot/command/admin/State.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ import org.fuchss.objectcasket.objectpacker.port.Session
/**
* A [BotCommand] that prints the current state of the bot to discord.
*/
class State(private val config: BotConfiguration, private val scheduler: Scheduler, private val session: Session) : GlobalCommand {
class State(
private val config: BotConfiguration,
private val scheduler: Scheduler,
private val session: Session
) : GlobalCommand {
override val permissions: CommandPermissions get() = CommandPermissions.ADMIN

override fun createCommand(): SlashCommandData {
return Commands.slash("state", "print the state of the bot")
}
override fun createCommand(): SlashCommandData = Commands.slash("state", "print the state of the bot")

override fun handle(event: SlashCommandInteraction) {
var msg = ""
Expand All @@ -49,7 +51,15 @@ class State(private val config: BotConfiguration, private val scheduler: Schedul
}

msg += "Registered Users: ${findUsers(event.jda, session, event.guild)}"
event.replyEmbeds(EmbedBuilder().setTitle("Current State").setDescription(msg).setColor(Constants.BLUE).build()).setEphemeral(true).queue()
event
.replyEmbeds(
EmbedBuilder()
.setTitle("Current State")
.setDescription(msg)
.setColor(Constants.BLUE)
.build()
).setEphemeral(true)
.queue()
}

private fun findUsers(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import org.fuchss.deltabot.utils.extensions.unhideAll
class UnhideAll : GlobalCommand {
override val permissions: CommandPermissions get() = CommandPermissions.ADMIN

override fun createCommand(): SlashCommandData {
return Commands.slash("unhide-all", "unhide all hidden messages")
}
override fun createCommand(): SlashCommandData = Commands.slash("unhide-all", "unhide all hidden messages")

override fun handle(event: SlashCommandInteraction) {
event.reply("Unhiding all messages ..").setEphemeral(true).complete()
Expand Down
19 changes: 13 additions & 6 deletions src/main/kotlin/org/fuchss/deltabot/command/user/Help.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ import org.fuchss.objectcasket.objectpacker.port.Session
/**
* A [BotCommand] that prints a temporary help message.
*/
open class Help(private val configuration: BotConfiguration, private val session: Session, protected val registry: ICommandRegistry) : GlobalCommand {
open class Help(
private val configuration: BotConfiguration,
private val session: Session,
protected val registry: ICommandRegistry
) : GlobalCommand {
override val permissions: CommandPermissions get() = CommandPermissions.ALL

override fun createCommand(): SlashCommandData {
return Commands.slash("help", "Prints a help message")
}
override fun createCommand(): SlashCommandData = Commands.slash("help", "Prints a help message")

override fun handle(event: SlashCommandInteraction) {
val visibilities = mutableListOf(CommandPermissions.ALL)
Expand Down Expand Up @@ -53,12 +55,17 @@ open class Help(private val configuration: BotConfiguration, private val session
message += "**/${cmd.name}**: ${cmd.description}\n"
val subcommands = cmd.subcommands
if (subcommands.isNotEmpty()) {
for (subcommand in subcommands)
for (subcommand in subcommands) {
message += "→ **${subcommand.name}**: ${subcommand.description}\n"
}
}
}

return EmbedBuilder().setTitle("$botName Help").setDescription(message.trim()).setColor(Constants.BLUE).build()
return EmbedBuilder()
.setTitle("$botName Help")
.setDescription(message.trim())
.setColor(Constants.BLUE)
.build()
}
}
}
Loading

0 comments on commit 3735f38

Please sign in to comment.