Revert "Rework code [re]execution flow"

This reverts commit 2584d0628d.
This commit is contained in:
PureFunctor 2021-07-28 23:41:23 +08:00
parent 2584d0628d
commit 172fd4cb3e
No known key found for this signature in database
GPG Key ID: AB04BE5510E8073B
4 changed files with 24 additions and 62 deletions

View File

@ -21,7 +21,6 @@ You can edit this file as you like.
, "node-process"
, "prelude"
, "psci-support"
, "refs"
, "strings"
, "stringutils"
]

View File

@ -17,11 +17,6 @@ exports.onMessageUpdate = (callback) => (client) => () => {
client.on("messageUpdate", (oldMsg, newMsg) => callback(oldMsg)(newMsg)())
}
exports.offMessageUpdate = (client) => () => {
// TODO: use removeListener?
client.removeAllListeners("messageUpdate");
}
exports.loginImpl = token => client => () => {
return client.login(token).then(() => client)
}

View File

@ -8,7 +8,7 @@ import Effect.Aff (Aff)
import Effect.Class (liftEffect)
import Prim.Row (class Lacks)
foreign import data Client ∷ Row Type -> Type
foreign import data Client ∷ #Type -> Type
foreign import newClient ∷ Effect (Client ())
@ -40,7 +40,6 @@ type Message =
{ channel ∷ Channel
, content ∷ String {- not really... -}
, author ∷ User
, id ∷ String {- Snowflake ~ String -}
}
foreign import removeAllReactionsImpl ∷ Message -> Effect (Promise Unit)
@ -80,10 +79,6 @@ foreign import onMessage ∷
foreign import onMessageUpdate ∷
∀ r. (Message -> Message -> Effect Unit) -> Client ( loggedIn ∷ Void | r ) -> Effect Unit
foreign import offMessageUpdate ::
∀ r. Client ( loggedIn :: Void | r ) -> Effect Unit
foreign import sendStringImpl ∷ NonEmptyString -> Channel -> Effect (Promise Unit)
sendString ∷ NonEmptyString -> Channel -> Aff Unit

View File

@ -9,24 +9,23 @@ import Data.Foldable (intercalate)
import Data.Interpolate (i)
import Data.Maybe (Maybe(..))
import Data.String (Pattern(..), contains, drop, length, stripPrefix, stripSuffix, trim)
import Data.String.NonEmpty as NES
import Data.String.NonEmpty.Internal (NonEmptyString(..))
import Data.String.NonEmpty.Internal as NES
import Data.String.Utils (startsWith)
import Discord.DiscordJS (DiscordToken, Message, createDMChannel, login, newClient, offMessageUpdate, onMessage, onMessageUpdate, react, sendString)
import Effect.Aff (Aff, Milliseconds(..), attempt, delay, error, forkAff, killFiber, launchAff_, message)
import Discord.DiscordJS (ChannelType(..), DiscordToken, Message, createDMChannel, getChannelType, login, newClient, onMessage, onMessageUpdate, react, removeAllReactions, sendString)
import Effect.Aff (Aff, attempt, launchAff_, message)
import Effect.Class (liftEffect)
import Effect.Class.Console (log)
import Effect.Ref as Effect
import Yoga.APIClient (CompileResult, YogaToken, compileAndRun)
_BOT_USER_ID ∷ String
_BOT_USER_ID = "625255925627748363"
_BOT_USER_ID = "741940687263760475"
mentionPrefix1 ∷ String
mentionPrefix1 = i "<@!" _BOT_USER_ID ">"
_OTHER_BOT_USER_ID ∷ String
_OTHER_BOT_USER_ID = "625255925627748363"
_OTHER_BOT_USER_ID = "741945029056135179"
mentionPrefix2 ∷ String
mentionPrefix2 = i "<@&" _OTHER_BOT_USER_ID ">"
@ -42,58 +41,32 @@ runBot ∷ YogaToken -> DiscordToken -> Aff Unit
runBot yogaToken discordToken = do
newClient <- newClient # liftEffect
client <- newClient # login discordToken
client # onMessage (launchAff_ <$> messageHandler client) # liftEffect
client # onMessage (launchAff_ <$> messageHandler) # liftEffect
client # onMessageUpdate (launchAff_ `map map map` messageUpdatedHandler) # liftEffect
where
messageHandler client msg@{ content } = printErrors msg do
messageUpdatedHandler _ msg = printErrors msg do
when (getChannelType msg.channel == TextChannel) $ removeAllReactions msg
msg # react rewindEmoji
messageHandler msg
messageHandler msg@{ content, channel } = printErrors msg do
when (isMention content) do
parseAndRun msg
-- Create a countdown
stopFR <- do
stopF <- forkAff stopSession
Effect.new stopF # liftEffect
let recvMessage old new =
when (msg.id == old.id && msg.id == new.id) do
-- Kill countdown
stopF_ <- Effect.read stopFR # liftEffect
killFiber (error "messageHandler: reset timer") stopF_
-- Renew countdown
stopF <- forkAff stopSession
Effect.write stopF stopFR # liftEffect
parseAndRun new
-- Register to messageUpdate
client # onMessageUpdate (launchAff_ `map map map` recvMessage) # liftEffect
where
-- Unregister from messageUpdate
stopSession = do
delay (Milliseconds 10_000.0)
client # offMessageUpdate # liftEffect
parseAndRun msg@{ channel, content } = do
case parseCodeBlock content of
Nothing -> do
sendBasicInstructions msg
Just code -> do
res <- compileAndRun yogaToken { code: prepareCode code }
case res of
Left cr ->
sendCompileProblem msg code cr
Right rr ->
case NES.fromString rr.stdout, NES.fromString rr.stderr of
Just _, _ -> do
case parseCodeBlock content of
Nothing -> do
sendBasicInstructions msg
Just code -> do
res <- compileAndRun yogaToken { code: prepareCode code }
case res of
Left cr -> sendCompileProblem msg code cr
Right rr -> case NES.fromString rr.stdout, NES.fromString rr.stderr of
Just stdout, _ -> do
msg # react robotMuscleEmoji
channel # sendString (NonEmptyString (prepareOutput rr.stdout))
Nothing, Just _ -> do
Nothing, Just stderr -> do
msg # react sirenEmoji
sendRunProblem msg code rr.stderr
_, _ -> channel # sendString (NonEmptyString "Something is weird")
sendBasicInstructions :: Message -> Aff Unit
sendBasicInstructions msg = do
msg # react thinkingEmoji