Skip to content

Commit

Permalink
Merge pull request #16 from SantioMC/refactor/remove-db
Browse files Browse the repository at this point in the history
Move to in-memory database
  • Loading branch information
SantioMC authored Apr 25, 2024
2 parents 9603e3e + e96d6b5 commit 17f0605
Show file tree
Hide file tree
Showing 12 changed files with 270 additions and 304 deletions.
37 changes: 20 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ The following is a full list of commands that MinehutUtils provides:
| `marketplace` | Either request or offer your services ||
| `cooldown info` | View server and your cooldown ||
| `cooldown` | Manage user's cooldowns ||
| `settings` | Change the bot settings or behaviour ||

## Getting Started

Expand Down Expand Up @@ -118,27 +117,27 @@ If you want to run the bot without a container, you can do so by following these
### Setup
The bot by default should now have most things working, however a bit of configuration is needed
to get both the marketplace and advertisement commands setup, along with a few other settings.
For more advanced configuration, see the [environment variables](#environment-variables)
to get both the marketplace and advertisement commands setup. By default, if the [environment variables](#environment-variables)
are not setup for `MARKET_CHANNEL` or `ADVERT_CHANNEL` their respective commands won't work and will be disabled.

#### Marketplace
To get the marketplace working, you should first create a channel for it if you haven't already.
Once you have a channel selected, use `/settings marketplace channel:<channel>` to set the channel.
Once you have a channel created, set `MARKET_CHANNEL` to the ID of the channel.
After that, users should be able to post both offers and requests in the channel.
#### Advertise
To get the advertise command working, create a channel for it like before, and run the following command:
`/settings advertise channel:<channel>`
To get advertisements working, you should first create a channel for it if you haven't already.
Once you have a channel created, set `ADVERT_CHANNEL` to the ID of the channel.

After that, users should be able to post advertisements in the channel.

#### Cooldowns
Setting a cooldown for either the advertise or marketplace command is easy. Simply run the following command:
```
/settings cooldown advertise cooldown:<time>
/settings cooldown marketplace cooldown:<time>
```
Cooldowns are pretty simple to set up and by default will fall back to 24 hours if they aren't
specified. Setting a custom duration can be done by setting `MARKET_COOLDOWN` or `ADVERT_COOLDOWN`
to a duration. Durations follow [ms format](https://www.npmjs.com/package/ms), so a value of `10m` is valid.
You're able to put any short-form time value in the cooldown, for example `1d`, `1h` or `30m`.
Valid Examples: `10s`, `10m`, `10h`, `10d`
#### Logs
By default, the bot will log to any channel appropriately named `#logs`, however this channel can be
Expand All @@ -148,11 +147,15 @@ any non-existent channel, a good example of this is `LOG_CHANNEL=.` *(Channels c
## Environment Variables
Below are a list of all possible environment variables that can be set to configure the bot.
| Variable | Description | Default |
|-------------|-----------------------------------------------|---------|
| DB_FILE | What should the database file be named | data.db |
| TOKEN | The bot token | |
| LOG_CHANNEL | The channel to post logs to *(without the #)* | logs |
| Variable | Description | Default |
|-----------------|----------------------------------------------------|---------|
| DB_FILE | What should the database file be named | data.db |
| TOKEN | The bot token | |
| LOG_CHANNEL | The channel to post logs to *(without the #)* | logs |
| MARKET_CHANNEL | The channel to where marketplace postings are sent | |
| ADVERT_CHANNEL | The channel to where advertisements are sent | |
| MARKET_COOLDOWN | The cooldown for the marketplace command | 24h |
| ADVERT_COOLDOWN | The cooldown for the advertisement command | 24h |
## Contributing
Thanks for everyone who has already contributed, and anyone willing to. This is a community bot and
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ dependencies {

// Libraries
implementation("org.reflections:reflections:0.10.2")
implementation("me.santio.Coffee:jda:85d9b1e6d5")
implementation("me.santio.Coffee:jda:5ee44b745d")
implementation("app.cash.sqldelight:sqlite-driver:2.1.0-SNAPSHOT")

// Adventure API
Expand Down
13 changes: 0 additions & 13 deletions src/main/kotlin/me/santio/minehututils/MinehutUtils.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package me.santio.minehututils

import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver
import me.santio.coffee.common.Coffee
import me.santio.coffee.jda.CoffeeJDA
import me.santio.minehututils.adapters.DurationAdapter
Expand All @@ -10,21 +8,10 @@ import me.santio.minehututils.minehut.Minehut
import me.santio.minehututils.utils.EnvUtils.env
import net.dv8tion.jda.api.JDA
import net.dv8tion.jda.api.JDABuilder
import java.io.File
import me.santio.minehututils.db.Minehut as Database

lateinit var bot: JDA
lateinit var database: Database

fun main() {
// Setup database
val dbFile = File(env("DB_FILE", "data.db"))
if (!dbFile.exists()) dbFile.createNewFile()

val driver: SqlDriver = JdbcSqliteDriver("jdbc:sqlite:${dbFile.absolutePath}")
Database.Schema.create(driver)
database = Database(driver)

// Create JDA instance
bot = JDABuilder.createDefault(
env("TOKEN") ?: throw IllegalStateException("No token provided")
Expand Down
33 changes: 23 additions & 10 deletions src/main/kotlin/me/santio/minehututils/commands/AdvertiseCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import me.santio.coffee.jda.annotations.Description
import me.santio.coffee.jda.gui.button.Button
import me.santio.coffee.jda.gui.showModal
import me.santio.minehututils.cooldown.Cooldown
import me.santio.minehututils.database
import me.santio.minehututils.ext.*
import me.santio.minehututils.cooldown.CooldownRegistry
import me.santio.minehututils.data.Channel
import me.santio.minehututils.ext.reply
import me.santio.minehututils.ext.sendMessage
import me.santio.minehututils.factories.EmbedFactory
import me.santio.minehututils.modals.AdvertisementModal
import me.santio.minehututils.resolvers.AutoModResolver
Expand All @@ -23,22 +25,22 @@ class AdvertiseCommand {
fun advertise(e: SlashCommandInteractionEvent) {
val guild = e.guild ?: return
val member = e.member ?: return
val settings = database.guildSettingsQueries.from(guild.id).executeAsOneOrNull()

if (settings?.advertChannel == null) {
e.reply(EmbedFactory.error("This server hasn't setup server advertisements!", guild)).setEphemeral(true).queue()

val advertChannel = Channel.ADVERTISEMENTS.get() ?: run {
e.reply(EmbedFactory.error("This server hasn't setup a advertisement channel!", e.guild!!)).setEphemeral(true).queue()
return
}

val channel = guild.getTextChannelById(settings.advertChannel) ?: run {
val channel = guild.getTextChannelById(advertChannel) ?: run {
e.reply(EmbedFactory.error("Failed to find the advertisement channel, was it deleted?", guild)).setEphemeral(true).queue()
return
}

// Check if they're on cooldown
if (Cooldown.ADVERTISE_USER.get(member)?.isElapsed() == false) {
val cooldown = CooldownRegistry.getCooldown(member.id, Cooldown.Kind.ADVERTISEMENT_USER)
if (cooldown != null) {
e.reply(EmbedFactory.error(
"You are currently on cooldown, try again ${Cooldown.ADVERTISE_USER.get(member)?.toTime() ?: "in a few seconds"}",
"You are currently on cooldown, try again in ${cooldown.remaining()}",
guild
)).setEphemeral(true).queue()
return
Expand All @@ -55,6 +57,15 @@ class AdvertiseCommand {
return@thenAccept
}

val serverCooldown = CooldownRegistry.getCooldown(m.server.id, Cooldown.Kind.ADVERTISEMENT_SERVER)
if (serverCooldown != null) {
e.reply(EmbedFactory.error(
"This server is currently on cooldown, try again in ${serverCooldown.remaining()}",
guild
)).setEphemeral(true).queue()
return@thenAccept
}

if (m.description.lines().size > 25) {
event.reply(EmbedFactory.error(
"Your description is too long, please shorten it to 25 lines or less.",
Expand All @@ -63,7 +74,9 @@ class AdvertiseCommand {
return@thenAccept
}

Cooldown.ADVERTISE_USER.set(member, Duration.ofSeconds(settings.advertCooldown).toCooldown())
CooldownRegistry.setCooldown(member.id, Cooldown.Kind.ADVERTISEMENT_USER)
CooldownRegistry.setCooldown(m.server.id, Cooldown.Kind.ADVERTISEMENT_SERVER)

channel.sendMessage(EmbedFactory.default("""
| ${EmojiResolver.find(guild, "minehut")?.formatted ?: ""} **${m.server.name}**
|
Expand Down
Loading

0 comments on commit 17f0605

Please sign in to comment.