package Ġorg . random cat . ag or abot . listener Ċ Ċ import Ġkotlinx . collections . immutable . Immutable Map Ċ import Ġkotlinx . collections . immutable . to Immutable Map Ċ import Ġnet . dv 8 tion . j da . api . events . message . Message Received Event Ċ import Ġorg . random cat . ag or abot . util . dis allow Ment ions ĊĊ Ċ interface ĠCommand Registry Ġ{ ĊĠĠĠ Ġ/** ĊĠĠĠĠ Ġ* ĠInv okes Ġthe Ġspecified Ġcommand ĊĠĠĠĠ Ġ*/ ĊĠĠĠ Ġfun Ġinvoke Command ( event : ĠMessage Received Event , Ġinvocation : ĠCommand Invocation ) Ċ } Ċ Ċ interface ĠQuery able Command Registry Ġ: ĠCommand Registry Ġ{ ĊĠĠĠ Ġfun Ġcommands (): ĠImmutableMap < String , ĠCommand > Ċ } Ċ Ċ interface ĠCommand Ġ{ ĊĠĠĠ Ġfun Ġinvoke ( event : ĠMessage Received Event , Ġinvocation : ĠCommand Invocation ) Ċ } Ċ Ċ type alias ĠUnknown Command Hook Ġ= Ġ( Message Received Event , ĠCommand Invocation ) Ġ-> ĠUnit Ċ Ċ private Ġfun Ġdefault Unknown Command ( event : ĠMessage Received Event , Ġcommand Invocation : ĠCommand Invocation ) Ġ{ ĊĠĠĠ Ġevent . channel . sendMessage (" Unknown Ġcommand Ġ\" ${ command Invocation . command }\" . "). dis allow Ment ions (). queue () Ċ } Ċ Ċ class ĠMutable Map Command Registry ( ĊĠĠĠ Ġregistry : ĠMap < String , ĠCommand >, ĊĠĠĠ Ġprivate Ġval Ġunknown Command Hook : ĠUnknown Command Hook Ġ= Ġ:: default Unknown Command , Ċ ) Ġ: ĠQuery able Command Registry Ġ{ ĊĠĠĠ Ġprivate Ġval Ġregistry : ĠMutable Map < String , ĠCommand > Ġ= Ġregistry . to Mutable Map () Ġ// ĠDef ensive Ġcopy ĊĊĠĠĠ Ġfun Ġadd Command ( name : ĠString , Ġcommand : ĠCommand ) Ġ{ ĊĠĠĠĠĠĠĠ Ġrequire (! registry . containsKey ( name )) Ġ{ Ġ" Cannot Ġinsert Ġduplicate Ġcommand Ġ$ name " Ġ} ĊĠĠĠĠĠĠĠ Ġregistry [ name ] Ġ= Ġcommand ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġadd Commands ( map : ĠMap < String , ĠCommand >) Ġ{ ĊĠĠĠĠĠĠĠ Ġmap . forEach Ġ{ Ġadd Command ( it . key , Ġit . value ) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġcommands (): ĠImmutableMap < String , ĠCommand > Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġregistry . to Immutable Map () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġinvoke Command ( event : ĠMessage Received Event , Ġinvocation : ĠCommand Invocation ) Ġ{ ĊĠĠĠĠĠĠĠ Ġregistry [ invocation . command ]?. invoke ( event , Ġinvocation ) Ġ?: Ġunknown Command Hook ( event , Ġinvocation ) ĊĠĠĠ Ġ} Ċ } Ċ