Skip to content

Commit e23a2bd

Browse files
committed
make objectstorage more usable, add guildOnly to SubCommand annotation
1 parent 16be5af commit e23a2bd

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

src/main/kotlin/me/devoxin/flight/api/CommandClient.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,6 @@ class CommandClient(
263263
return false
264264
}
265265

266-
if (!ctx.isFromGuild && props.guildOnly) {
267-
dispatchSafely { it.onCheckFailed(ctx, cmd, CheckType.GUILD_CHECK) }
268-
return false
269-
}
270-
271266
if (ctx.isFromGuild) {
272267
if (props.userPermissions.isNotEmpty()) {
273268
val userCheck = props.userPermissions.filterNot { ctx.member!!.hasPermission(ctx.guildChannel!!, it) }
@@ -291,6 +286,13 @@ class CommandClient(
291286
dispatchSafely { it.onCheckFailed(ctx, cmd, CheckType.NSFW_CHECK) }
292287
return false
293288
}
289+
} else {
290+
val subcommandProperties = (ctx.invokedCommand as? SubCommandFunction)?.properties
291+
292+
if (props.guildOnly || subcommandProperties?.guildOnly == true) {
293+
dispatchSafely { it.onCheckFailed(ctx, cmd, CheckType.GUILD_CHECK) }
294+
return false
295+
}
294296
}
295297

296298
return eventListeners.all { it.onCommandPreInvoke(ctx, cmd) }

src/main/kotlin/me/devoxin/flight/api/annotations/SubCommand.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ package me.devoxin.flight.api.annotations
1212
@Target(AnnotationTarget.FUNCTION)
1313
annotation class SubCommand(
1414
val aliases: Array<String> = [],
15-
val description: String = "No description available"
15+
val description: String = "No description available",
16+
val guildOnly: Boolean = false
1617
)

src/main/kotlin/me/devoxin/flight/api/entities/ObjectStorage.kt

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
package me.devoxin.flight.api.entities
22

33
import java.util.concurrent.ConcurrentHashMap
4+
import kotlin.collections.MutableMap.MutableEntry
45

5-
class ObjectStorage {
6+
class ObjectStorage : MutableMap<String, Any> {
67
private val map = ConcurrentHashMap<String, Any>()
78

8-
val size: Int
9+
override val size: Int
910
get() = map.size
1011

12+
override val entries: MutableSet<MutableEntry<String, Any>>
13+
get() = map.entries
14+
15+
override val keys: MutableSet<String>
16+
get() = map.keys
17+
18+
override val values: MutableCollection<Any>
19+
get() = map.values
20+
1121
operator fun set(key: String, value: Any) {
1222
map[key] = value
1323
}
1424

15-
operator fun get(key: String): Any? {
25+
override operator fun get(key: String): Any? {
1626
return map[key]
1727
}
1828

@@ -28,10 +38,23 @@ class ObjectStorage {
2838

2939
@Suppress("UNCHECKED_CAST")
3040
fun <T : Any> computeIfAbsent(key: String, initializer: (String) -> T): T {
31-
return map.computeIfAbsent(key, initializer) as T
41+
return map.computeIfAbsent(key, initializer) as? T
42+
?: throw IllegalStateException("Computed object is not of type T!")
3243
}
3344

34-
fun clear() {
45+
override fun clear() {
3546
map.clear()
3647
}
48+
49+
override fun isEmpty() = map.isEmpty()
50+
51+
override fun remove(key: String): Any? = map.remove(key)
52+
53+
override fun putAll(from: Map<out String, Any>) = map.putAll(from)
54+
55+
override fun put(key: String, value: Any): Any? = map.put(key, value)
56+
57+
override fun containsValue(value: Any) = map.containsValue(value)
58+
59+
override fun containsKey(key: String) = map.containsKey(key)
3760
}

0 commit comments

Comments
 (0)