@@ -9,24 +9,23 @@ import Data.Foldable (intercalate)
99import Data.Interpolate (i)
1010import Data.Maybe (Maybe(..))
1111import Data.String (Pattern(..), contains, drop, length, stripPrefix, stripSuffix, trim)
12−import Data.String.NonEmpty as NES
1312import Data.String.NonEmpty.Internal (NonEmptyString(..))
13+import Data.String.NonEmpty.Internal as NES
1414import Data.String.Utils (startsWith)
15−import Discord.DiscordJS (DiscordToken, Message, createDMChannel, login, newClient, offMessageUpdate, onMessage, onMessageUpdate, react, sendString)
16−import Effect.Aff (Aff, Milliseconds(..), attempt, delay, error, forkAff, killFiber, launchAff_, message)
15+import Discord.DiscordJS (ChannelType(..), DiscordToken, Message, createDMChannel, getChannelType, login, newClient, onMessage, onMessageUpdate, react, removeAllReactions, sendString)
16+import Effect.Aff (Aff, attempt, launchAff_, message)
1717import Effect.Class (liftEffect)
1818import Effect.Class.Console (log)
19−import Effect.Ref as Effect
2019import Yoga.APIClient (CompileResult, YogaToken, compileAndRun)
2120
2221_BOT_USER_ID ∷ String
23−_BOT_USER_ID = "625255925627748363"
22+_BOT_USER_ID = "741940687263760475"
2423
2524mentionPrefix1 ∷ String
2625mentionPrefix1 = i "<@!" _BOT_USER_ID ">"
2726
2827_OTHER_BOT_USER_ID ∷ String
29−_OTHER_BOT_USER_ID = "625255925627748363"
28+_OTHER_BOT_USER_ID = "741945029056135179"
3029
3130mentionPrefix2 ∷ String
3231mentionPrefix2 = i "<@&" _OTHER_BOT_USER_ID ">"
@@ -42,58 +41,32 @@ runBot ∷ YogaToken -> DiscordToken -> Aff Unit
4241runBot yogaToken discordToken = do
4342 newClient <- newClient # liftEffect
4443 client <- newClient # login discordToken
45− client # onMessage (launchAff_ <$> messageHandler client) # liftEffect
44+ client # onMessage (launchAff_ <$> messageHandler) # liftEffect
45+ client # onMessageUpdate (launchAff_ `map map map` messageUpdatedHandler) # liftEffect
4646 where
47− messageHandler client msg@{ content } = printErrors msg do
47+ messageUpdatedHandler _ msg = printErrors msg do
48+ when (getChannelType msg.channel == TextChannel) $ removeAllReactions msg
49+ msg # react rewindEmoji
50+ messageHandler msg
51+
52+ messageHandler msg@{ content, channel } = printErrors msg do
4853 when (isMention content) do
49− parseAndRun msg
50−
51− -- Create a countdown
52− stopFR <- do
53− stopF <- forkAff stopSession
54− Effect.new stopF # liftEffect
55−
56− let recvMessage old new =
57− when (msg.id == old.id && msg.id == new.id) do
58− -- Kill countdown
59− stopF_ <- Effect.read stopFR # liftEffect
60− killFiber (error "messageHandler: reset timer") stopF_
61−
62− -- Renew countdown
63− stopF <- forkAff stopSession
64− Effect.write stopF stopFR # liftEffect
65−
66− parseAndRun new
67−
68− -- Register to messageUpdate
69− client # onMessageUpdate (launchAff_ `map map map` recvMessage) # liftEffect
70−
71− where
72− -- Unregister from messageUpdate
73− stopSession = do
74− delay (Milliseconds 10_000.0)
75− client # offMessageUpdate # liftEffect
76−
77− parseAndRun msg@{ channel, content } = do
78− case parseCodeBlock content of
79− Nothing -> do
80− sendBasicInstructions msg
81− Just code -> do
82− res <- compileAndRun yogaToken { code: prepareCode code }
83− case res of
84− Left cr ->
85− sendCompileProblem msg code cr
86− Right rr ->
87− case NES.fromString rr.stdout, NES.fromString rr.stderr of
88− Just _, _ -> do
54+ case parseCodeBlock content of
55+ Nothing -> do
56+ sendBasicInstructions msg
57+ Just code -> do
58+ res <- compileAndRun yogaToken { code: prepareCode code }
59+ case res of
60+ Left cr -> sendCompileProblem msg code cr
61+ Right rr -> case NES.fromString rr.stdout, NES.fromString rr.stderr of
62+ Just stdout, _ -> do
8963 msg # react robotMuscleEmoji
9064 channel # sendString (NonEmptyString (prepareOutput rr.stdout))
91− Nothing, Just _ -> do
65+ Nothing, Just stderr -> do
9266 msg # react sirenEmoji
9367 sendRunProblem msg code rr.stderr
9468 _, _ -> channel # sendString (NonEmptyString "Something is weird")
9569
96−
9770sendBasicInstructions :: Message -> Aff Unit
9871sendBasicInstructions msg = do
9972 msg # react thinkingEmoji