git.iko.soy/rowtype-yoga/royo
on main help sign in
Revert "Rework code [re]execution flow"

This reverts commit 2584d0628d3c4206e6adb0c088af07021881b515.

commit172fd4cb3ee5cc9398df4cda76275adcf5ceb945 authorPureFunctor <purefunctor@gmail.com> dateWed 28 Jul 2021, 23:41 (5 years ago) parent2584d06
4 files changed+24−62
spago.dhall−1 src/Discord/DiscordJS.js−5 src/Discord/DiscordJS.purs+1−6 src/Royo/Discord/Bot.purs+23−50
spago.dhall−1
@@ -21,7 +21,6 @@ You can edit this file as you like.
2121 , "node-process"
2222 , "prelude"
2323 , "psci-support"
24− , "refs"
2524 , "strings"
2625 , "stringutils"
2726 ]
src/Discord/DiscordJS.js−5
@@ -17,11 +17,6 @@ exports.onMessageUpdate = (callback) => (client) => () => {
1717 client.on("messageUpdate", (oldMsg, newMsg) => callback(oldMsg)(newMsg)())
1818}
1919
20−exports.offMessageUpdate = (client) => () => {
21− // TODO: use removeListener?
22− client.removeAllListeners("messageUpdate");
23−}
24−
2520exports.loginImpl = token => client => () => {
2621 return client.login(token).then(() => client)
2722}
src/Discord/DiscordJS.purs+1−6
@@ -8,7 +8,7 @@ import Effect.Aff (Aff)
88import Effect.Class (liftEffect)
99import Prim.Row (class Lacks)
1010
11−foreign import data Client ∷ Row Type -> Type
11+foreign import data Client ∷ #Type -> Type
1212
1313foreign import newClient ∷ Effect (Client ())
1414
@@ -40,7 +40,6 @@ type Message =
4040 { channel ∷ Channel
4141 , content ∷ String {- not really... -}
4242 , author ∷ User
43− , id ∷ String {- Snowflake ~ String -}
4443 }
4544
4645foreign import removeAllReactionsImpl ∷ Message -> Effect (Promise Unit)
@@ -80,10 +79,6 @@ foreign import onMessage ∷
8079foreign import onMessageUpdate ∷
8180 ∀ r. (Message -> Message -> Effect Unit) -> Client ( loggedIn ∷ Void | r ) -> Effect Unit
8281
83−foreign import offMessageUpdate ::
84− ∀ r. Client ( loggedIn :: Void | r ) -> Effect Unit
85−
86−
8782foreign import sendStringImpl ∷ NonEmptyString -> Channel -> Effect (Promise Unit)
8883
8984sendString ∷ NonEmptyString -> Channel -> Aff Unit
src/Royo/Discord/Bot.purs+23−50
@@ -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