Skip to content

Commit

Permalink
Reduce .nn usage by creating wrapper for getLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
ScoreUnder committed Jul 14, 2021
1 parent 1d51b84 commit 359f571
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package score.discord.canti.collections

import org.slf4j.LoggerFactory
import score.discord.canti.collections.CacheCoordinator.logger
import score.discord.canti.wrappers.NullWrappers.loggerOf

import scala.collection.mutable
import scala.concurrent.ExecutionContext.Implicits.global
Expand Down Expand Up @@ -65,7 +65,7 @@ class CacheCoordinator[K, V](cache: CacheLayer[K, Option[V]], backend: AsyncMap[
end CacheCoordinator

object CacheCoordinator:
private[CacheCoordinator] val logger = LoggerFactory.getLogger(getClass).nn
private[CacheCoordinator] val logger = loggerOf[this.type]

extension [K, V](me: AsyncMap[K, V])
infix def withCache(cache: CacheLayer[K, Option[V]]): CacheCoordinator[K, V] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package score.discord.canti.functionality
import net.dv8tion.jda.api.entities.Message
import net.dv8tion.jda.api.events.GenericEvent
import net.dv8tion.jda.api.hooks.EventListener
import org.slf4j.LoggerFactory
import score.discord.canti.collections.{MessageCache, ReplyCache}
import score.discord.canti.command.Command
import score.discord.canti.functionality.ownership.MessageOwnership
Expand Down Expand Up @@ -38,7 +37,7 @@ end Commands
class Commands(using MessageCache, ReplyCache, MessageOwnership) extends EventListener:
import Commands.*

private val logger = LoggerFactory.getLogger(classOf[Commands]).nn
private val logger = loggerOf[Commands]
// All commands and aliases, indexed by name
private val commands = mutable.HashMap[String, Command]()
// Commands list excluding aliases
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import net.dv8tion.jda.api.events.message.react.*
import net.dv8tion.jda.api.events.role.update.RoleUpdatePositionEvent
import net.dv8tion.jda.api.events.user.GenericUserEvent
import net.dv8tion.jda.api.hooks.EventListener
import org.slf4j.LoggerFactory
import score.discord.canti.functionality.ownership.MessageOwnership
import score.discord.canti.util.StringUtils.{formatMessageForLog as formatMessage}
import score.discord.canti.wrappers.NullWrappers.*
Expand All @@ -41,7 +40,7 @@ import scala.concurrent.ExecutionContext.Implicits.global
import scala.language.reflectiveCalls

class EventLogger(using messageOwnership: MessageOwnership) extends EventListener:
private val logger = LoggerFactory.getLogger(classOf[EventLogger]).nn
private val logger = loggerOf[EventLogger]

private def logHigherIfMyMessage(ev: GenericMessageEvent, logLine: String): Unit =
given JDA = ev.getJDA
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import net.dv8tion.jda.api.interactions.commands.{CommandInteraction, OptionType
import net.dv8tion.jda.api.requests.ErrorResponse.UNKNOWN_CHANNEL
import net.dv8tion.jda.api.requests.restaction.ChannelAction
import net.dv8tion.jda.api.{JDA, Permission}
import org.slf4j.LoggerFactory
import score.discord.canti.collections.{AsyncMap, ReplyCache}
import score.discord.canti.command.slash.SlashCommand
import score.discord.canti.command.{Command, ReplyingCommand}
Expand Down Expand Up @@ -53,7 +52,7 @@ class PrivateVoiceChats(
eventWaiter: EventWaiter,
)(using messageOwnership: MessageOwnership, replyCache: ReplyCache)
extends EventListener:
private val logger = LoggerFactory.getLogger(classOf[PrivateVoiceChats]).nn
private val logger = loggerOf[PrivateVoiceChats]

private val invites = ConcurrentHashMap[GuildUserId, Invite]()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import net.dv8tion.jda.api.events.interaction.SlashCommandEvent
import net.dv8tion.jda.api.events.{GenericEvent, ReadyEvent}
import net.dv8tion.jda.api.hooks.EventListener
import net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction
import org.slf4j.LoggerFactory
import score.discord.canti.command.slash.SlashCommand
import score.discord.canti.wrappers.NullWrappers.*
import score.discord.canti.wrappers.jda.Conversions.{richGuild, richMessageChannel, richUser}
Expand All @@ -14,7 +13,7 @@ import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

class SlashCommands(commands: SlashCommand*) extends EventListener:
private val logger = LoggerFactory.getLogger(classOf[SlashCommands]).nn
private val logger = loggerOf[SlashCommands]

val commandsMap: Map[String, SlashCommand] =
commands.map(c => normaliseCommandName(c.name) -> c).toMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import cps.monads.FutureAsyncMonad
import net.dv8tion.jda.api.entities.{Message, MessageChannel, TextChannel, User}
import net.dv8tion.jda.api.events.GenericEvent
import net.dv8tion.jda.api.hooks.EventListener
import org.slf4j.LoggerFactory
import score.discord.canti.collections.{AsyncMap, ReplyCache}
import score.discord.canti.command.Command
import score.discord.canti.functionality.ownership.MessageOwnership
Expand All @@ -29,7 +28,7 @@ class Spoilers(spoilerTexts: AsyncMap[ID[Message], String], conversations: Conve
MessageOwnership,
ReplyCache
) extends EventListener:
private val logger = LoggerFactory.getLogger(classOf[Spoilers]).nn
private val logger = loggerOf[Spoilers]

val spoilerEmote = "🔍"

Expand Down
3 changes: 1 addition & 2 deletions src/main/scala/score/discord/canti/util/APIHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package score.discord.canti.util
import net.dv8tion.jda.api.entities.{Message, MessageChannel}
import net.dv8tion.jda.api.exceptions.{ErrorResponseException, PermissionException}
import net.dv8tion.jda.api.requests.{ErrorResponse, RestAction}
import org.slf4j.LoggerFactory
import score.discord.canti.collections.ReplyCache
import score.discord.canti.functionality.ownership.MessageOwnership
import score.discord.canti.wrappers.NullWrappers.*
Expand All @@ -19,7 +18,7 @@ import scala.util.chaining.*

/** Miscellaneous functions useful when dealing with JDA's API calls */
object APIHelper:
private val logger = LoggerFactory.getLogger(getClass).nn
private val logger = loggerOf[this.type]

/** Curried function to report an exception to the console.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package score.discord.canti.wrappers

import org.slf4j.LoggerFactory

import scala.reflect.ClassTag

object NullWrappers:
def loggerOf[T: ClassTag] = LoggerFactory.getLogger(summon[ClassTag[T]].runtimeClass).nn

extension [T](me: T | Null)
inline def ? : Option[T] =
if me != null then Some(me) else None
Expand Down

0 comments on commit 359f571

Please sign in to comment.