Skip to content

Commit

Permalink
Add ImportFile command
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeJMattson committed Mar 23, 2020
1 parent 9f5961e commit 831c7ed
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
19 changes: 10 additions & 9 deletions commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,16 @@
| UpdateTarget | (Channel), Message ID | Replace the target message embed with the loaded embed. |

## Core
| Commands | Arguments | Description |
| --------- | ------------------------ | ------------------------------------------ |
| Create | Embed Name | Create a new embed with this name. |
| Delete | (Embed) | Delete the embed with this name. |
| Duplicate | Embed Name, (Embed) | Create a new embed from an existing embed. |
| Export | (Embed) | Export the currently loaded embed to JSON. |
| Import | Embed Name, JSON | Import a JSON String as an embed. |
| Load | Embed | Load the embed with this name into memory. |
| Send | (Channel), (shouldTrack) | Send the currently loaded embed. |
| Commands | Arguments | Description |
| ---------- | ------------------------ | ------------------------------------------ |
| Create | Embed Name | Create a new embed with this name. |
| Delete | (Embed) | Delete the embed with this name. |
| Duplicate | Embed Name, (Embed) | Create a new embed from an existing embed. |
| Export | (Embed) | Export the currently loaded embed to JSON. |
| Import | Embed Name, JSON | Import a JSON String as an embed. |
| ImportFile | Embed Name, JSON | |
| Load | Embed | Load the embed with this name into memory. |
| Send | (Channel), (shouldTrack) | Send the currently loaded embed. |

## Edit
| Commands | Arguments | Description |
Expand Down
45 changes: 31 additions & 14 deletions src/main/kotlin/me/jakejmattson/embedbot/commands/CoreCommands.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package me.jakejmattson.embedbot.commands

import com.google.gson.JsonSyntaxException
import me.aberrantfox.kjdautils.api.annotation.CommandSet
import me.aberrantfox.kjdautils.api.dsl.command.commands
import me.aberrantfox.kjdautils.api.dsl.command.*
import me.aberrantfox.kjdautils.api.dsl.embed
import me.aberrantfox.kjdautils.internal.arguments.*
import me.jakejmattson.embedbot.arguments.EmbedArg
import me.jakejmattson.embedbot.dataclasses.CopyLocation
Expand Down Expand Up @@ -90,22 +91,19 @@ fun coreCommands(embedService: EmbedService) = commands {
description = messages.descriptions.IMPORT
execute(WordArg("Embed Name"), SentenceArg("JSON")) {
val (name, json) = it.args
val guild = it.guild!!

if (guild.hasEmbedWithName(name))
return@execute it.respond(messages.errors.EMBED_ALREADY_EXISTS)
it.importJson(name, json, embedService)
}
}

try {
val embed = createEmbedFromJson(name, json)
val wasAdded = embedService.addEmbed(guild, embed)
command("ImportFile") {
description = ""
execute(WordArg("Embed Name"), FileArg("JSON")) {
val (name, jsonFile) = it.args
val json = jsonFile.readText()

if (!wasAdded)
it.respond(messages.errors.EMBED_ALREADY_EXISTS)
it.importJson(name, json, embedService)

it.reactSuccess()
} catch (e: JsonSyntaxException) {
it.respond("Invalid JSON! ${e.message?.substringAfter("Exception: ")}")
}
jsonFile.delete()
}
}

Expand All @@ -123,4 +121,23 @@ fun coreCommands(embedService: EmbedService) = commands {
it.respond("```json\n$json```")
}
}
}

private fun CommandEvent<*>.importJson(name: String, json: String, embedService: EmbedService) {
val guild = guild!!

if (guild.hasEmbedWithName(name))
return respond(messages.errors.EMBED_ALREADY_EXISTS)

try {
val embed = createEmbedFromJson(name, json)
val wasAdded = embedService.addEmbed(guild, embed)

if (!wasAdded)
respond(messages.errors.EMBED_ALREADY_EXISTS)

reactSuccess()
} catch (e: JsonSyntaxException) {
respond("Invalid JSON! ${e.message?.substringAfter("Exception: ")}")
}
}

0 comments on commit 831c7ed

Please sign in to comment.