Skip to content

Commit

Permalink
Merge pull request #4 from daksh7011/feature/torrent-info
Browse files Browse the repository at this point in the history
Add torrent info command
  • Loading branch information
daksh7011 authored Aug 11, 2024
2 parents 61214a7 + 5492f4f commit c4f44d2
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 11 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ on:

pull_request:

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
build:
runs-on: ubuntu-latest
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ on:
schedule:
- cron: '45 23 * * 6'

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
analyze:
name: Analyze (${{ matrix.language }})
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/qodana_code_quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ on:
branches:
- master

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
qodana:
runs-on: ubuntu-latest
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# qBittorret Discrd Bot
Simple discord bot built around qBitTorrent REST API. Powered by [Kord](https://github.com/kordlib/kord)
# qBitTorrent Discord Bot
Simple discord bot built around qBitTorrent REST API. Built with Kotlin, Powered by [Kord](https://github.com/kordlib/kord)

[Discord Invite](https://discord.com/oauth2/authorize?client_id=1271843928060203031&permissions=563364418284608&integration_type=0&scope=bot)
2 changes: 0 additions & 2 deletions detekt.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# TODO: Update `rootPackage` in naming -> InvalidPackageDeclaration

build:
maxIssues: 0
excludeCorrectable: false
Expand Down
2 changes: 0 additions & 2 deletions src/main/kotlin/qbtbot/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import qbtbot.util.PresenceManager
import qbtbot.util.loginToBackend
import qbtbot.util.testGuild

// inv link: https://discord.com/oauth2/authorize?client_id=1271843928060203031&permissions=563364418284608&integration_type=0&scope=bot

suspend fun main() {
val bot = ExtensibleBot(env(Environment.TOKEN)) {
chatCommands {
Expand Down
18 changes: 15 additions & 3 deletions src/main/kotlin/qbtbot/commands/TorrentInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import qbtbot.model.Torrent
import qbtbot.util.BASE_URL
import qbtbot.util.bold
import qbtbot.util.httpClient
import qbtbot.util.round

@Suppress("MagicNumber") // TODO: Remove
class TorrentInfo : Extension() {
override val name: String = "Torrent Info"
override suspend fun setup() {
Expand All @@ -19,9 +21,19 @@ class TorrentInfo : Extension() {
check { failIf(event.message.author == null) }
action {
val torrents: List<Torrent> = httpClient.get("$BASE_URL/torrents/info").body()
val response = torrents.groupBy({ it.category }, { it.name })
.map { (category, name) -> "${category.bold()} \n\n ${name.joinToString(separator = "\n")}" }
.joinToString("\n\n")
val response = torrents.groupBy({ it.category }, { it })
.map { (category, torrents) ->
"Category: ${category.bold()}\n${
torrents.joinToString(separator = "\n") {
"${
it.name.substring(
0,
it.name.length.coerceAtMost(50)
)
} is ${(it.progress * 100).round()}%"
}
}"
}.joinToString(separator = "\n\n\n")
message.respond(response)
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/main/kotlin/qbtbot/util/Extension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ import dev.kord.core.entity.Message
import dev.kord.rest.builder.message.EmbedBuilder
import io.ktor.client.request.forms.submitForm
import io.ktor.http.parameters
import kotlin.math.round

val testGuild: Snowflake get() = Snowflake(env(Environment.TEST_GUILD_ID).toLong())

fun Message.isBot(): Boolean = author?.isBot != false
fun Message.isNotBot(): Boolean = isBot().not()

Check warning on line 16 in src/main/kotlin/qbtbot/util/Extension.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused symbol

Function "isNotBot" is never used
fun Snowflake.isOwner(): Boolean = toString() == env(Environment.OWNER_ID)

Check warning on line 17 in src/main/kotlin/qbtbot/util/Extension.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused symbol

Function "isOwner" is never used

suspend fun Message.getEmbedFooter(): EmbedBuilder.Footer = this.kord.prepareEmbed()
suspend fun Message.getEmbedFooter(): EmbedBuilder.Footer = this.kord.prepareEmbedFooter()

Check warning on line 19 in src/main/kotlin/qbtbot/util/Extension.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused symbol

Function "getEmbedFooter" is never used

suspend fun Kord.prepareEmbed(): EmbedBuilder.Footer = EmbedBuilder.Footer().apply {
suspend fun Kord.prepareEmbedFooter(): EmbedBuilder.Footer = EmbedBuilder.Footer().apply {
text = "Powered by ${getUser(selfId)?.username}"
icon = getUser(selfId)?.avatar?.cdnUrl?.toUrl()
}
Expand All @@ -37,3 +38,10 @@ suspend fun ExtensibleBot.loginToBackend() {
)
logger.info { "qBitTorrent login -> status: ${response.status}" }
}

@Suppress("MagicNumber")
fun Double.round(decimals: Int = 2): Double {
var multiplier = 1.0
repeat(decimals) { multiplier *= 10 }
return round(this * multiplier) / multiplier
}

0 comments on commit c4f44d2

Please sign in to comment.