Skip to content

Commit

Permalink
Documentation?
Browse files Browse the repository at this point in the history
Signed-off-by: Sanduh32 <[email protected]>
  • Loading branch information
Sanduhr32 committed Jan 18, 2018
1 parent a18f2a9 commit c361050
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/main/java/ml/duncte123/skybot/SkyBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static void main(String... args) throws Exception {
AirUtils.loadAllTags();

//Set the token to a string
String token = AirUtils.config.getString("discord.token", "Your Bot Token");
String token = AirUtils.config.getString("discord.tokenu", "Your Bot Token");

//But this time we are going to shard it
int TOTAL_SHARDS = AirUtils.config.getInt("discord.totalShards", 1);
Expand Down
137 changes: 120 additions & 17 deletions src/main/kotlin/ml/duncte123/skybot/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,23 @@ import java.time.OffsetDateTime
import java.util.*

@SinceSkybot("3.51.5")
@DocumentationNeeded
@DocumentationNeeded("Array<T>.random()", "List<T>.random()", "Set<T>.random()", "Map<K, V>.random()")
@Author(nickname = "Sanduhr32", author = "Maurice R S")
class EarthUtils {
companion object {

/**
*
* This function generates a debug JSON that can help us to improve errors if we hide them.
*
* @param throwable a [Throwable] that provides data.
* @returns a [JSONObject] that contains all given details.
*
*
* @see [EarthUtils.throwableArrayToJSONArray]
* @see [EarthUtils.stacktraceArrayToJSONArray]
* @see [EarthUtils.stackTraceToJSONObject]
*/
@JvmStatic
fun throwableToJSONObject(throwable: Throwable): JSONObject {
return JSONObject().put("className", throwable::class.java.name)
Expand All @@ -57,14 +70,36 @@ class EarthUtils {
.put("stacktraces", stacktraceArrayToJSONArray(throwable.stackTrace))
}

/**
* This small function wraps [List]<[JSONObject]> into an [JSONArray]
*
*
* @see [EarthUtils.throwableToJSONObject]
* @see [EarthUtils.stacktraceArrayToJSONArray]
* @see [EarthUtils.stackTraceToJSONObject]
*/
@JvmStatic
private fun throwableArrayToJSONArray(throwables: Array<Throwable>) =
JSONArray(throwables.map { throwableToJSONObject(it) })

/**
* This tiny function wraps [List]<[JSONObject]> into an [JSONArray]
*
*
* @see [EarthUtils.throwableToJSONObject]
* @see [EarthUtils.stackTraceToJSONObject]
*/
@JvmStatic
private fun stacktraceArrayToJSONArray(stackTraces: Array<StackTraceElement>): JSONArray =
JSONArray(stackTraces.map { stackTraceToJSONObject(it) })

/**
* This is just a smaller function that converts [StackTraceElement]s into [JSONObject] that we use in the see tag
*
*
* @see [EarthUtils.throwableToJSONObject]
* @see [EarthUtils.stacktraceArrayToJSONArray]
*/
@JvmStatic
private fun stackTraceToJSONObject(stackTraceElement: StackTraceElement) =
JSONObject().put("className", stackTraceElement.className)
Expand All @@ -73,6 +108,7 @@ class EarthUtils {
.put("isNative", stackTraceElement.isNativeMethod)

@JvmStatic
@Deprecated(message = "The following code may be removed!", level = DeprecationLevel.WARNING)
fun write(a_file: String, content: String) {
val file = File(a_file)

Expand All @@ -82,45 +118,112 @@ class EarthUtils {
FileOutputStream(file).write(content.toByteArray())
}

/**
*
* This function wraps any [JDA] object in a delegate of it.
* It also takes the highest possible delegate.
*
* @param jdaObject is any possible [JDA] object
* @returns a possibly null delegate of any [JDA] object we have implemented
*/
@JvmStatic
fun delegateOf(jdaobject: Any?): Any? {
return when (jdaobject) {
is Category -> CategoryDelegate(jdaobject)
is TextChannel -> TextChannelDelegate(jdaobject)
is VoiceChannel -> VoiceChannelDelegate(jdaobject)
is Channel -> ChannelDelegate(jdaobject)
is Guild -> GuildDelegate(jdaobject)
is JDA -> JDADelegate(jdaobject)
is Member -> MemberDelegate(jdaobject)
is Presence -> PresenceDelegate(jdaobject)
is Role -> RoleDelegate(jdaobject)
is User -> UserDelegate(jdaobject)
@Deprecated(message = "The following code may be removed!", level = DeprecationLevel.HIDDEN)
fun delegateOf(jdaObject: Any): Any? {
return when (jdaObject) {
is Category -> CategoryDelegate(jdaObject)
is TextChannel -> TextChannelDelegate(jdaObject)
is VoiceChannel -> VoiceChannelDelegate(jdaObject)
is Channel -> ChannelDelegate(jdaObject)
is Guild -> GuildDelegate(jdaObject)
is JDA -> JDADelegate(jdaObject)
is Member -> MemberDelegate(jdaObject)
is Presence -> PresenceDelegate(jdaObject)
is Role -> RoleDelegate(jdaObject)
is User -> UserDelegate(jdaObject)
else -> {
null
}
}
}

/**
*
* This function generates a debug JSON that can help us to improve audio and memory issues.
*
* @returns a [JSONObject] that contains all given details.
*
*
* @see [EarthUtils.gMMtoJSON]
* @see [EarthUtils.playerToJSON]
* @see [EarthUtils.schedulerToJSON]
* @see [EarthUtils.trackToJSON]
*/
@JvmStatic
fun audioJSON(): JSONObject {
val json = JSONObject().put("time", OffsetDateTime.now())
AirUtils.audioUtils.musicManagers.entries.forEach { json.put(it.key, JSONObject().put("guildId", it.key).put("manager", gMMtoJSON(it.value))) }
return json
}

/**
* This tiny function converts a [GuildMusicManager] into a [JSONObject]
*
* @param manager a [GuildMusicManager] that provides data.
* @returns a [JSONObject] with all the converted data.
*
*
* @see [EarthUtils.audioJSON]
* @see [EarthUtils.playerToJSON]
* @see [EarthUtils.schedulerToJSON]
* @see [EarthUtils.trackToJSON]
*/
@JvmStatic
private fun gMMtoJSON(manager: GuildMusicManager): JSONObject =
JSONObject().put("player", playerToJSON(manager.player)).put("scheduler", schedulerToJSO(manager.scheduler))
JSONObject().put("player", playerToJSON(manager.player)).put("scheduler", schedulerToJSON(manager.scheduler))

/**
* This is a little function that converts a [AudioPlayer] into a [JSONObject]
*
* @param player a [AudioPlayer] that provides data.
* @returns a [JSONObject] with all the converted data.
*
*
* @see [EarthUtils.audioJSON]
* @see [EarthUtils.gMMtoJSON]
* @see [EarthUtils.schedulerToJSON]
* @see [EarthUtils.trackToJSON]
*/
@JvmStatic
private fun playerToJSON(player: AudioPlayer): JSONObject =
JSONObject().put("currentTrack", player.playingTrack?.let { trackToJSON(it) }).put("paused",player.isPaused)
.put("volume", player.volume)

/**
* This smaller function converts a [TrackScheduler] into a [JSONObject]
*
* @param scheduler a [TrackScheduler] that provides data.
* @returns a [JSONObject] with all the converted data.
*
*
* @see [EarthUtils.audioJSON]
* @see [EarthUtils.gMMtoJSON]
* @see [EarthUtils.playerToJSON]
* @see [EarthUtils.trackToJSON]
*/
@JvmStatic
private fun schedulerToJSO(scheduler: TrackScheduler): JSONObject =
private fun schedulerToJSON(scheduler: TrackScheduler): JSONObject =
JSONObject().put("repeating", scheduler.isRepeating).put("queue_size", scheduler.queue.size)

/**
* This small function that converts a [AudioTrack] into a [JSONObject]
*
* @param track a [AudioTrack] that provides data.
* @returns a [JSONObject] with all the converted data.
*
*
* @see [EarthUtils.audioJSON]
* @see [EarthUtils.gMMtoJSON]
* @see [EarthUtils.playerToJSON]
* @see [EarthUtils.schedulerToJSON]
*/
@JvmStatic
private fun trackToJSON(track: AudioTrack): JSONObject =
JSONObject().put("source", track.sourceManager.sourceName).put("position", track.position)
Expand Down

0 comments on commit c361050

Please sign in to comment.