Skip to content
This repository was archived by the owner on Jun 21, 2024. It is now read-only.

Commit 014ecda

Browse files
author
Mark Nefedov
committed
basic disconnect handling, log4j-slf4j-impl, readme update.
1 parent dc388b6 commit 014ecda

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
# Simple Discord bot in Kotlin
22
A simple bot for recording voice chats.
33

4-
Usage:
4+
For simple recording:
55
```
66
::record time
77
::stop
88
```
9+
"Instant replay" functions much like Nvidia's ShadowPlay instant replays (that is where name come from). Bot is constantly recording audio from a channel to a buffer and will send current buffer content in a file when you request it.
10+
```
11+
:!record
12+
:!replay
13+
```
914

1015
After recording bot will send an audio file to the chat it was started from.
1116

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dependencies {
1414
compile "net.dv8tion:JDA:4.1.1_101"
1515
compile group: 'ws.schild', name: 'jave-all-deps', version: '2.7.3'
1616
compile group: 'org.apache.commons', name: 'commons-collections4', version: '4.4'
17+
compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.13.1'
1718
}
1819

1920
compileKotlin {

src/main/kotlin/InstantReplayCommandHandler.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import net.dv8tion.jda.api.audio.AudioReceiveHandler
22
import net.dv8tion.jda.api.audio.CombinedAudio
3+
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceLeaveEvent
34
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent
45
import net.dv8tion.jda.api.hooks.ListenerAdapter
56
import org.apache.commons.collections4.queue.CircularFifoQueue
@@ -28,6 +29,15 @@ class InstantReplayCommandHandler : ListenerAdapter() {
2829
event.message.textChannel.sendFile(mp3Compressed).submit().thenRunAsync { mp3Compressed.delete() }
2930
}
3031
}
32+
33+
override fun onGuildVoiceLeave(event: GuildVoiceLeaveEvent) {
34+
super.onGuildVoiceLeave(event)
35+
if (event.member.id == clientId)
36+
{
37+
println("Bot leave from: ${event.channelLeft.name}")
38+
event.guild.audioManager.receivingHandler = null
39+
}
40+
}
3141
}
3242

3343
class InstantReplayAudioHandler : AudioReceiveHandler {

src/main/kotlin/main.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
11
import net.dv8tion.jda.api.JDABuilder
22

3+
lateinit var clientId : String
4+
35
fun main(args : Array<String>) {
46
val token = when {
57
args.isNotEmpty() -> args[0]
68
System.getenv("BOT_TOKEN") != null -> System.getenv("BOT_TOKEN")
79
else -> {
8-
print("Bot token:")
10+
println("Bot token:")
911
(readLine() ?: return)
1012
}
1113
}
14+
15+
clientId = when {
16+
args.size > 1 -> args[1]
17+
System.getenv("CLIENT_ID") != null -> System.getenv("CLIENT_ID")
18+
else -> {
19+
println("Client id: ")
20+
(readLine() ?: return)
21+
}
22+
}
23+
24+
println("Starting bot")
1225
JDABuilder(token).apply {
1326
addEventListeners(
1427
RecordCommandHandler(),
1528
InstantReplayCommandHandler()
1629
)
30+
setAutoReconnect(true)
1731
build()
1832
}
1933
}

0 commit comments

Comments
 (0)