Skip to content

Commit e56aac5

Browse files
Simon NaumovShuffleZZZ
andauthored
♻️ logging everywhere (#138)
* ♻️ logging everywhere refactoring (flume like) * ⬆️ version bump * 🐛 escaping markdown in messages * 🚨 barkers gonna bark * ✅ fixing tests * ⬆️ messages from the past, version bumpгора * 💩🍺 barker barker widest mouth... * 💩🍺 should work now clueless * 💩🍺 update deprecated poops * ⬆️ upgrade gradle Co-authored-by: ShuffleZZZ <mihpihnaty@yandex.ru>
1 parent a02aa2e commit e56aac5

File tree

22 files changed

+169
-93
lines changed

22 files changed

+169
-93
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## [1.2.2] - 18.04.2022
4+
5+
### Fixed
6+
7+
- Using logging everywhere
8+
- Escaping markdown in messages
9+
310
## [1.2.1] - 04.04.2022
411

512
### Added
@@ -110,6 +117,7 @@
110117
- Implicit party pulling
111118
- Rude mode
112119

120+
[1.2.2]: https://github.com/pool-party/pull-party-bot/compare/v1.2.1...v1.2.2
113121
[1.2.1]: https://github.com/pool-party/pull-party-bot/compare/v1.2.0...v1.2.1
114122
[1.2.0]: https://github.com/pool-party/pull-party-bot/compare/v1.1.0.2...v1.2.0
115123
[1.1.0.2]: https://github.com/pool-party/pull-party-bot/compare/v1.1.0.1...v1.1.0.2

Procfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
web: java -jar build/libs/pull-party-bot-1.2.1.jar
1+
web: java -jar build/libs/pull-party-bot-1.2.2.jar

build.gradle.kts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,46 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
22

33
plugins {
44
id("java")
5-
id("org.jetbrains.kotlin.jvm") version "1.5.21"
6-
kotlin("plugin.serialization") version "1.5.21"
5+
id("org.jetbrains.kotlin.jvm") version "1.6.20"
6+
kotlin("plugin.serialization") version "1.6.20"
77

8-
id("org.flywaydb.flyway") version "7.12.0"
8+
id("org.flywaydb.flyway") version "8.5.8"
99
}
1010

1111
group = "org.pool-party"
12-
version = "1.2.1"
12+
version = "1.2.2"
1313

1414
repositories {
1515
maven("https://jitpack.io")
1616
mavenCentral()
1717
}
1818

19-
val exposedVersion = "0.32.1"
20-
val testContainersVersion = "1.16.0"
21-
val jupyterVersion = "5.6.0"
22-
val kotlinVersion = "1.5.21"
19+
val exposedVersion = "0.38.1"
20+
val testContainersVersion = "1.17.1"
21+
val jupyterVersion = "5.8.2"
22+
val kotlinVersion = "1.6.20"
2323

2424
dependencies {
2525
implementation("org.jetbrains.kotlin", "kotlin-stdlib-jdk8", kotlinVersion)
2626
implementation("org.jetbrains.kotlin", "kotlin-reflect", kotlinVersion)
27-
implementation("org.jetbrains.kotlinx", "kotlinx-serialization-json", "1.2.2")
27+
implementation("org.jetbrains.kotlinx", "kotlinx-serialization-json", "1.3.2")
2828
implementation("com.github.elbekD", "kt-telegram-bot", "1.4.1")
2929

3030
implementation("org.jetbrains.exposed", "exposed-core", exposedVersion)
3131
implementation("org.jetbrains.exposed", "exposed-dao", exposedVersion)
3232
implementation("org.jetbrains.exposed", "exposed-jdbc", exposedVersion)
3333
implementation("org.jetbrains.exposed", "exposed-jodatime", exposedVersion)
3434

35-
implementation("org.flywaydb", "flyway-core", "7.12.0")
35+
implementation("org.flywaydb", "flyway-core", "8.5.8")
3636

3737
implementation("com.natpryce", "konfig", "1.6.10.0")
3838

39-
implementation("org.slf4j", "slf4j-simple", "2.0.0-alpha2")
40-
implementation("io.github.microutils", "kotlin-logging", "2.0.10")
39+
implementation("org.slf4j", "slf4j-simple", "2.0.0-alpha7")
40+
implementation("io.github.microutils", "kotlin-logging", "2.1.21")
4141

4242
implementation("info.debatty", "java-string-similarity", "2.0.0")
4343

44-
runtimeOnly("org.postgresql", "postgresql", "42.2.23")
44+
runtimeOnly("org.postgresql", "postgresql", "42.3.4")
4545

4646
testImplementation("org.jetbrains.kotlin", "kotlin-test-junit5", kotlinVersion)
4747
testImplementation("org.junit.jupiter", "junit-jupiter-api", jupyterVersion)
@@ -73,6 +73,6 @@ tasks.test {
7373

7474
tasks.withType<KotlinCompile>().configureEach {
7575
kotlinOptions.jvmTarget = "1.8"
76-
kotlinOptions.freeCompilerArgs += "-Xopt-in=kotlinx.coroutines.DelicateCoroutinesApi"
77-
kotlinOptions.freeCompilerArgs += "-Xopt-in=kotlinx.serialization.ExperimentalSerializationApi"
76+
kotlinOptions.freeCompilerArgs += "-opt-in=kotlinx.coroutines.DelicateCoroutinesApi"
77+
kotlinOptions.freeCompilerArgs += "-opt-in=kotlinx.serialization.ExperimentalSerializationApi"
7878
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1.1-all.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-all.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

src/main/kotlin/com/github/pool_party/pull_party_bot/commands/Command.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,12 @@ abstract class CaseCommand(command: String, description: String, helpMessage: St
7777
protected fun Bot.sendCaseMessage(
7878
chatId: Long,
7979
message: String,
80-
parseMode: String? = null,
8180
replyTo: Long? = null,
8281
markup: ReplyKeyboard? = null
8382
) =
84-
sendMessage(
83+
sendMessageLogging(
8584
chatId,
8685
if (chatDao.getRude(chatId)) message.uppercase() else message,
87-
parseMode,
8886
replyTo = replyTo,
8987
markup = markup
9088
)
@@ -104,15 +102,15 @@ fun Bot.validateAdministrator(user: User?, chat: Chat, sendMessage: Boolean = tr
104102
val chatId = chat.id
105103

106104
if (user == null) {
107-
sendMessage(chatId, ON_SENDER_FAIL, "Markdown")
105+
sendMessageLogging(chatId, ON_SENDER_FAIL)
108106
return false
109107
}
110108

111109
val chatType = chat.type
112110
if ((chatType == "group" || chatType == "supergroup") &&
113111
getChatAdministrators(chatId).join().all { it.user != user }
114112
) {
115-
if (sendMessage) sendMessage(chatId, ON_PERMISSION_DENY, "Markdown")
113+
if (sendMessage) sendMessageLogging(chatId, ON_PERMISSION_DENY)
116114
return false
117115
}
118116
return true
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.github.pool_party.pull_party_bot.commands
2+
3+
import com.elbekD.bot.Bot
4+
import com.elbekD.bot.types.Message
5+
import com.elbekD.bot.types.ReplyKeyboard
6+
import mu.KotlinLogging
7+
import java.util.concurrent.CompletableFuture
8+
9+
private val logger = KotlinLogging.logger { }
10+
11+
fun <T> CompletableFuture<T>.logging(prefix: String = ""): CompletableFuture<T> = handleAsync { value, throwable ->
12+
if (throwable != null) {
13+
logger.error { "$prefix: ${throwable.message}:\n${throwable.stackTraceToString()}" }
14+
throw throwable
15+
}
16+
value
17+
}
18+
19+
private fun String.escape(symbols: String) = replace("[$symbols]".toRegex()) { "\\${it.groupValues[0]}" }
20+
21+
private fun String.escapeSpecial() = escape("-!.<>\\(\\)")
22+
23+
fun String.escapeMarkdown() = escape("-!.<>\\(\\)_*\\[\\]`")
24+
25+
fun Bot.sendMessageLogging(
26+
chatId: Long,
27+
text: String,
28+
markup: ReplyKeyboard? = null,
29+
replyTo: Long? = null
30+
): CompletableFuture<out Message> {
31+
logger.debug { "Sending '$text'" }
32+
return sendMessage(chatId, text.escapeSpecial(), "MarkdownV2", replyTo = replyTo, markup = markup)
33+
.logging("Failed to send message \"$text\"")
34+
}
35+
36+
fun Bot.deleteMessageLogging(chatId: Long, messageId: Long): CompletableFuture<out Boolean> {
37+
logger.debug { "Deleting message $chatId/$messageId" }
38+
return deleteMessage(chatId, messageId).logging("Failed to delete message $chatId/$messageId")
39+
}
40+
41+
fun Bot.answerCallbackQueryLogging(id: String, text: String? = null): CompletableFuture<out Boolean> {
42+
logger.debug { "Answering callback query $id" }
43+
return answerCallbackQuery(id, text).logging("Answering callback query $id")
44+
}

src/main/kotlin/com/github/pool_party/pull_party_bot/commands/handlers/AliasCommand.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,39 @@ import com.github.pool_party.pull_party_bot.commands.messages.ON_ALIAS_PARSE_FAI
99
import com.github.pool_party.pull_party_bot.commands.messages.ON_CREATE_REQUEST_FAIL
1010
import com.github.pool_party.pull_party_bot.commands.messages.ON_PARTY_NAME_FAIL
1111
import com.github.pool_party.pull_party_bot.commands.messages.onAliasSuccess
12+
import com.github.pool_party.pull_party_bot.commands.sendMessageLogging
1213
import com.github.pool_party.pull_party_bot.database.dao.AliasCreationResult
1314
import com.github.pool_party.pull_party_bot.database.dao.ChatDao
1415
import com.github.pool_party.pull_party_bot.database.dao.PartyDao
1516

1617
class AliasCommand(
1718
private val partyDao: PartyDao,
1819
chatDao: ChatDao,
19-
) : CaseCommand("alias", "create a party alias", HELP_ALIAS, chatDao) {
20+
) : CaseCommand("alias", "create a new party with the same users", HELP_ALIAS, chatDao) {
2021

2122
override suspend fun Bot.action(message: Message, args: String?) {
2223
val parsedArgs = parseArgs(args)
2324
val chatId = message.chat.id
2425

2526
if (parsedArgs.isNullOrEmpty() || parsedArgs.size != 2) {
26-
sendMessage(chatId, ON_ALIAS_PARSE_FAIL, "Markdown")
27+
sendMessageLogging(chatId, ON_ALIAS_PARSE_FAIL)
2728
return
2829
}
2930

3031
val (aliasName, partyName) = parsedArgs
3132

3233
if (!validatePartyName(aliasName)) {
33-
sendMessage(chatId, ON_PARTY_NAME_FAIL, "Markdown")
34+
sendMessageLogging(chatId, ON_PARTY_NAME_FAIL)
3435
return
3536
}
3637

37-
sendMessage(
38+
sendMessageLogging(
3839
chatId,
3940
when (partyDao.createAlias(chatId, aliasName, partyName)) {
4041
AliasCreationResult.SUCCESS -> onAliasSuccess(aliasName)
4142
AliasCreationResult.NAME_TAKEN -> ON_CREATE_REQUEST_FAIL
4243
AliasCreationResult.NO_PARTY -> ON_ALIAS_FAIL
4344
},
44-
"Markdown"
4545
)
4646
}
4747
}

src/main/kotlin/com/github/pool_party/pull_party_bot/commands/handlers/ChangePartyCommands.kt

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,37 @@ import com.github.pool_party.pull_party_bot.commands.messages.onAddSuccess
2020
import com.github.pool_party.pull_party_bot.commands.messages.onChangeSuccess
2121
import com.github.pool_party.pull_party_bot.commands.messages.onCreateSuccess
2222
import com.github.pool_party.pull_party_bot.commands.messages.onDeleteSuccess
23+
import com.github.pool_party.pull_party_bot.commands.sendMessageLogging
2324
import com.github.pool_party.pull_party_bot.database.dao.ChatDao
2425
import com.github.pool_party.pull_party_bot.database.dao.PartyDao
2526

2627
class CreateCommand(partyDao: PartyDao, chatDao: ChatDao) :
27-
AbstractChangeCommand("create", "create new party", HELP_CREATE, PartyChangeStatus.CREATE, partyDao, chatDao)
28+
AbstractChangeCommand(
29+
"create",
30+
"create new party with mentioned users",
31+
HELP_CREATE,
32+
PartyChangeStatus.CREATE,
33+
partyDao,
34+
chatDao
35+
)
2836

2937
class ChangeCommand(partyDao: PartyDao, chatDao: ChatDao) :
30-
AbstractChangeCommand("change", "changing existing party", HELP_CHANGE, PartyChangeStatus.CHANGE, partyDao, chatDao)
38+
AbstractChangeCommand(
39+
"change",
40+
"change an existing party",
41+
HELP_CHANGE,
42+
PartyChangeStatus.CHANGE,
43+
partyDao,
44+
chatDao
45+
)
3146

3247
class AddCommand(partyDao: PartyDao, chatDao: ChatDao) :
33-
AbstractChangeCommand("add", "add people to a party", HELP_ADD, PartyChangeStatus.ADD, partyDao, chatDao)
48+
AbstractChangeCommand("add", "add new users to the given party", HELP_ADD, PartyChangeStatus.ADD, partyDao, chatDao)
3449

3550
class RemoveCommand(partyDao: PartyDao, chatDao: ChatDao) :
3651
AbstractChangeCommand(
3752
"remove",
38-
"remove people from a party",
53+
"remove given users from the provided party",
3954
HELP_REMOVE,
4055
PartyChangeStatus.REMOVE,
4156
partyDao,
@@ -59,19 +74,18 @@ abstract class AbstractChangeCommand(
5974
// TODO suggest alias instead of party, if possible
6075

6176
if (parsedArgs.isNullOrEmpty() || parsedArgs.size < 2) {
62-
sendMessage(
77+
sendMessageLogging(
6378
chatId,
6479
if (status == PartyChangeStatus.CREATE) ON_CREATE_EMPTY
6580
else ON_CHANGE_EMPTY,
66-
"Markdown"
6781
)
6882
return
6983
}
7084

7185
val partyName = parsedArgs[0].removePrefix("@")
7286

7387
if (!validatePartyName(partyName)) {
74-
sendMessage(chatId, ON_PARTY_NAME_FAIL, "Markdown")
88+
sendMessageLogging(chatId, ON_PARTY_NAME_FAIL)
7589
return
7690
}
7791

@@ -86,12 +100,12 @@ abstract class AbstractChangeCommand(
86100
users = users.map { "@$it" }
87101

88102
if (status.changesFull && users.singleOrNull()?.removePrefix("@") == partyName) {
89-
sendMessage(chatId, ON_SINGLETON_PARTY, "Markdown")
103+
sendMessageLogging(chatId, ON_SINGLETON_PARTY)
90104
return
91105
}
92106

93107
if (failedUsers.isNotEmpty()) {
94-
sendMessage(chatId, ON_USERS_FAIL)
108+
sendMessageLogging(chatId, ON_USERS_FAIL)
95109

96110
if (users.isEmpty()) {
97111
sendMessage(
@@ -109,7 +123,7 @@ abstract class AbstractChangeCommand(
109123
return
110124
}
111125

112-
sendMessage(chatId, status.onFailure, "Markdown")
126+
sendMessageLogging(chatId, status.onFailure)
113127
}
114128
}
115129

src/main/kotlin/com/github/pool_party/pull_party_bot/commands/handlers/DeleteCommands.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,21 @@ import com.github.pool_party.pull_party_bot.commands.messages.ON_DELETE_EMPTY
1414
import com.github.pool_party.pull_party_bot.commands.messages.onAliasDeleteSuccess
1515
import com.github.pool_party.pull_party_bot.commands.messages.onPartyDeleteSuggest
1616
import com.github.pool_party.pull_party_bot.commands.messages.onPartyDeleteUnchanged
17+
import com.github.pool_party.pull_party_bot.commands.sendMessageLogging
1718
import com.github.pool_party.pull_party_bot.database.dao.ChatDao
1819
import com.github.pool_party.pull_party_bot.database.dao.PartyDao
1920
import kotlinx.serialization.encodeToString
2021
import kotlinx.serialization.json.Json
2122

2223
class DeleteCommand(private val partyDao: PartyDao, chatDao: ChatDao) :
23-
AdministratorCommand("delete", "forget the parties as they have never happened", HELP_DELETE, chatDao) {
24+
AdministratorCommand("delete", "delete the parties you provided", HELP_DELETE, chatDao) {
2425

2526
override fun Bot.mainAction(message: Message, args: String?) {
2627
val parsedArgs = parseArgs(args)?.distinct()
2728
val chatId = message.chat.id
2829

2930
if (parsedArgs.isNullOrEmpty()) {
30-
sendMessage(chatId, ON_DELETE_EMPTY, "Markdown")
31+
sendMessageLogging(chatId, ON_DELETE_EMPTY)
3132
return
3233
}
3334

@@ -81,6 +82,6 @@ class ClearCommand(chatDao: ChatDao) :
8182
override fun Bot.mainAction(message: Message, args: String?) {
8283
val chatId = message.chat.id
8384
chatDao.clear(chatId)
84-
sendMessage(chatId, ON_CLEAR_SUCCESS, "Markdown")
85+
sendMessageLogging(chatId, ON_CLEAR_SUCCESS)
8586
}
8687
}

src/main/kotlin/com/github/pool_party/pull_party_bot/commands/handlers/FeedbackCommand.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.github.pool_party.pull_party_bot.commands.AbstractCommand
77
import com.github.pool_party.pull_party_bot.commands.messages.HELP_FEEDBACK
88
import com.github.pool_party.pull_party_bot.commands.messages.ON_FEEDBACK_SUCCESS
99
import com.github.pool_party.pull_party_bot.commands.messages.onFeedback
10+
import com.github.pool_party.pull_party_bot.commands.sendMessageLogging
1011

1112
class FeedbackCommand : AbstractCommand("feedback", "share your ideas and experience with developers", HELP_FEEDBACK) {
1213

@@ -15,11 +16,11 @@ class FeedbackCommand : AbstractCommand("feedback", "share your ideas and experi
1516
val developChatId = Configuration.DEVELOP_CHAT_ID
1617
if (developChatId == 0L || parsedArgs.isNullOrBlank()) return
1718

18-
sendMessage(
19+
sendMessageLogging(
1920
developChatId,
2021
onFeedback(message.from?.username, message.chat.title) + parsedArgs
2122
)
2223

23-
sendMessage(message.chat.id, ON_FEEDBACK_SUCCESS, replyTo = message.message_id)
24+
sendMessageLogging(message.chat.id, ON_FEEDBACK_SUCCESS, replyTo = message.message_id)
2425
}
2526
}

0 commit comments

Comments
 (0)