Skip to content

Commit

Permalink
Add history for commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickster258 committed Aug 17, 2023
1 parent d06be3d commit d5451ff
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/main/kotlin/org/openredstone/chad/Chad.kt
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ fun main(args: Array<String>) = runBlocking {
chadConfig.greetings,
chadConfig.ingameBotRoleId,
chadConfig.gameChatChannelId,
this,
database,
this
)

if (chadConfig.enableNotificationRoles) NotificationManager(
Expand Down
21 changes: 14 additions & 7 deletions src/main/kotlin/org/openredstone/chad/DiscordListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ fun startDiscordListeners(
greetings: List<String>,
ingameBotRole: String,
gameChatChannelId: Long,
coroutineScope: CoroutineScope,
sql: Sql,
coroutineScope: CoroutineScope
) {
startDiscordCommandListener(discordApi, executor, ingameBotRole, gameChatChannelId, coroutineScope)
startDiscordCommandListener(discordApi, executor, ingameBotRole, gameChatChannelId, coroutineScope, sql)
if (disableSpoilers) {
startSpoilerListener(discordApi, coroutineScope)
}
Expand All @@ -50,6 +51,7 @@ private fun startDiscordCommandListener(
ingameBotRole: String,
gameChatChannelId: Long,
coroutineScope: CoroutineScope,
sql: Sql
) {
suspend fun onDiscordCommand(event: MessageCreateEvent) {
val server = event.server.toNullable()
Expand All @@ -60,7 +62,7 @@ private fun startDiscordCommandListener(
discordApi.getRoleById(ingameBotRole).toNullable() in user.getRoles(server) &&
!user.isYourself
) {
inGameListener(event, executor, coroutineScope)
inGameListener(event, executor, coroutineScope, sql)
}
if (user.isBot) {
return
Expand All @@ -70,15 +72,15 @@ private fun startDiscordCommandListener(
val roles = user.getRoles(server).map(Role::getName)
val username = user.getDisplayName(server)
val sender = Sender(username, roles)
response = executor.tryExecute(sender, event.message, event.messageContent, coroutineScope) ?: return
response = executor.tryExecute(sender, event.message, event.messageContent, coroutineScope, sql) ?: return
if (response.privateReply) {
user.sendMessage(snipped(response.reply)).await()
} else {
event.channel.sendMessage(snipped("$username: ${response.reply}")).await()
}
} else {
val sender = Sender(event.messageAuthor.name, emptyList())
response = executor.tryExecute(sender, event.message, event.messageContent, coroutineScope) ?: return
response = executor.tryExecute(sender, event.message, event.messageContent, coroutineScope, sql) ?: return
user.sendMessage(snipped(response.reply)).await()
}
for (reaction in response.reactions) {
Expand Down Expand Up @@ -115,11 +117,16 @@ private fun snipped(response: String) =

private val inGameRegex = Regex("""^`[A-Za-z]+` \*\*([A-Za-z0-9_\\]+)\*\*: (.*)$""")

private suspend fun inGameListener(event: MessageCreateEvent, executor: CommandExecutor, coroutineScope: CoroutineScope) {
private suspend fun inGameListener(
event: MessageCreateEvent,
executor: CommandExecutor,
coroutineScope: CoroutineScope,
sql: Sql
) {
val rawMessage = event.message.content
val (sender, message) = inGameRegex.matchEntire(rawMessage)?.destructured ?: return
val commandSender = Sender(sender.replace("\\", ""), emptyList())
val response = executor.tryExecute(commandSender, event.message, message, coroutineScope) ?: return
val response = executor.tryExecute(commandSender, event.message, message, coroutineScope, sql) ?: return
event.channel.sendMessage(
if (response.privateReply) {
"$sender: I can't private message to in-game yet!"
Expand Down
28 changes: 27 additions & 1 deletion src/main/kotlin/org/openredstone/chad/Sql.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,37 @@ object SqlCommand : Table("command") {
val response = varchar("cmd_response", 512)
}

object SqlHistory : Table("history") {
val key = varchar("hist_key", 128)
val args = varchar("hist_args", 512)
val response = varchar("hist_response", 512)
val time = integer("hist_time").index()
val service = varchar("hist_service", 128)
val user = varchar("hist_user", 64)
}

class Sql(file: String, driver: String = "org.sqlite.JDBC") {
private val database = Database.connect("jdbc:sqlite:$file", driver)

fun initTables() = transaction(database) {
SchemaUtils.create(SqlCommand)
SchemaUtils.create(SqlCommand, SqlHistory)
}

fun insertHistory(
key: String,
args: String,
response: String,
service: String,
user: String
) = transaction(database) {
SqlHistory.insert {
it[SqlHistory.key] = key
it[SqlHistory.args] = args
it[SqlHistory.response] = response
it[SqlHistory.time] = (System.currentTimeMillis() / 1000).toInt()
it[SqlHistory.service] = service
it[SqlHistory.user] = user
}
}

fun insertCommand(key: String, response: String) = transaction(database) {
Expand Down
33 changes: 31 additions & 2 deletions src/main/kotlin/org/openredstone/chad/commands/Command.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ class CommandExecutor(private val commandChar: Char, private val commands: Comma
val invalidArg = CommandResponse(false, "Invalid argument", emptyList())
}

suspend fun tryExecute(sender: Sender, discordMessage: Message, message: String, coroutineScope: CoroutineScope): CommandResponse? {
suspend fun tryExecute(
sender: Sender,
discordMessage: Message,
message: String,
coroutineScope: CoroutineScope,
sql: Sql
): CommandResponse? {
if (message.isEmpty() || message[0] != commandChar) {
return null
}
Expand All @@ -72,12 +78,35 @@ class CommandExecutor(private val commandChar: Char, private val commands: Comma
val command = commands[name] ?: return invalidCommand
val scope = ReplyScope(sender, discordMessage, coroutineScope)

return try {
val commandResponse = try {
command.runCommand(scope, args).takeIf { it.reply.isNotEmpty() }
} catch (e: Exception) {
logger.error(e) { "caught exception while running command" }
CommandResponse(command.privateReply, "An error occurred while running the command.")
}
// Insert command history
val discordAuthor = discordMessage.userAuthor.get()
var senderToInsert = sender.username
val service = if (discordAuthor.isBot) {
discordAuthor.name
} else {
senderToInsert = discordAuthor.idAsString
if (discordMessage.isPrivateMessage) {
"directmessage"
} else {
"discord <#${discordMessage.channel.id}>"
}
}
if (commandResponse != null) {
sql.insertHistory(
name,
args.joinToString(", "),
commandResponse.reply,
service,
senderToInsert
)
}
return commandResponse
}
}

Expand Down

0 comments on commit d5451ff

Please sign in to comment.