2R6RBGYN6CEXNP5PD4CYFXVGC4WY6CLNQ2P3NN6UU3I7PGD76FIQC fileRequest :: (MonadIO m) => Text -> m (Maybe ByteString)fileRequest input = runReq defaultHttpConfig $ douri <- mkURI $ Text.dropWhileEnd (== '&') inputcase useHttpsURI uri ofNothing -> return NothingJust (url, options) -> dobs <- req GET url NoReqBody bsResponse optionsreturn . Just $ responseBody bshandleDiff ::Interaction -> Maybe OptionsData -> Maybe ResolvedData -> DiscordHandler ()handleDiff intr opts resolved = dolet oldId = keyList >>= lookup "old"newId = keyList >>= lookup "new"atts = resolved >>= attachmentsoldUrl = atts >>= (\x -> oldId >>= \y -> lookup (showT y) x)newUrl = atts >>= (\x -> newId >>= \y -> lookup (showT y) x)case (oldUrl, newUrl) of(Just oldReq, Just newReq) -> dooldBytes <- fileRequest oldReqnewBytes <- fileRequest newReq
slashDiffCommand :: Maybe CreateApplicationCommandslashDiffCommand = options <$> createChatInput "diff" "Check the diff of two decklists"
case (oldBytes, newBytes) of(Just oldContent, Just newContent) ->let res =((,) <$> fromByteString oldContent <*> fromByteString newContent)<&> view . eval . uncurry diff . uncurry evalDiffsin case res ofLeft _ ->createResponse"Something went wrong, make sure the files are in the correct format"Right report -> createResponse report_ -> createResponse "Unable to fetch the files"_ -> createResponse "Malformed URI's"wherecreateResponse response =void $restCall $Request.CreateInteractionResponse(interactionId intr)(interactionToken intr)(interactionResponseBasic response)keyList = values =<< optsvalues (OptionsDataValues vals) =mapM( \caseOptionDataValueAttachment {optionDataValueName, optionDataValueAttachment} -> Just (optionDataValueName, optionDataValueAttachment)_ -> Nothing)valsvalues _ = Nothingattachments :: ResolvedData -> Maybe [(Text, Text)]attachments (ResolvedData {resolvedDataAttachments = vals}) = case vals ofNothing -> NothingJust val -> parseMaybe parseAttachments valparseAttachments = withObject "<attachment>" $ \obj ->let kvs = toList objparsed = mapM parseUrl kvsin parsedparseUrl = withObject "<url>" $ \obj -> doattachmentId <- obj .: "id"attachmentUrl <- obj .: "url"return (attachmentId, attachmentUrl)slashDiffCommand :: SlashCommandslashDiffCommand = SlashCommand "diff" mkOptions handleDiff
InteractionCreate intr -> liftIO $ print intr
InteractionCreate intr -> case intr ofcmd@InteractionApplicationCommand{ applicationCommandData =ApplicationCommandDataChatInput{ applicationCommandDataName, resolvedData, optionsData}} ->case find (\c -> applicationCommandDataName == name c) slashCommands ofNothing -> echo "The command could not be found in the commands, out of sync"Just found -> handler found cmd optionsData resolvedData_ -> pure ()
onReady appId = case slashDiffCommand ofNothing -> pure ()Just command -> dores <- restCall $ Request.CreateGlobalApplicationCommand appId commandcase res ofLeft e -> liftIO $ print eRight validCmds -> doliftIO $ putStrLn "Command successfully registered"registered <- restCall $ Request.GetGlobalApplicationCommands appIdcase registered ofLeft _ -> liftIO $ putStrLn "error occured"Right cmds -> dolet validId = applicationCommandId validCmdsoutdatedIds = filter (/= validId) . map applicationCommandId $ cmdsin forM_ outdatedIds $restCall . Request.DeleteGlobalApplicationCommand appId
onReady appId = doregisterCommands <- mapM tryRegistering slashCommandscase sequence registerCommands ofLeft err -> echo $ "[!] Unable to register some slash commands: " <> showT errRight cmds -> doecho $ "Registered " <> showT (length cmds) <> " command(s)"unregisterOutdatedCommands cmdswheretryRegistering cmd =case register cmd ofNothing -> return . Left $ RestCallErrorCode 0 "" ""Just reg -> restCall $ Request.CreateGlobalApplicationCommand appId regunregisterOutdatedCommands validCmds = doregistered <- restCall $ Request.GetGlobalApplicationCommands appId
case registered ofLeft err -> echo $ "Failed to get commands: " <> showT errRight cmds ->let validIds = map applicationCommandId validCmdsoutdatedIds = filter (`notElem` validIds) . map applicationCommandId $ cmdsin forM_ outdatedIds $ restCall . Request.DeleteGlobalApplicationCommand appId